/[svn]/qsampler/trunk/src/qsampler.cpp
ViewVC logotype

Diff of /qsampler/trunk/src/qsampler.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2181 by capela, Sun Jun 5 10:21:53 2011 UTC revision 2987 by capela, Fri Sep 23 11:42:50 2016 UTC
# Line 1  Line 1 
1  // qsampler.cpp  // qsampler.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2004-2010, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2004-2016, rncbc aka Rui Nuno Capela. All rights reserved.
5     Copyright (C) 2007, 2008 Christian Schoenebeck     Copyright (C) 2007, 2008 Christian Schoenebeck
6    
7     This program is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or
# Line 28  Line 28 
28  #include <QLibraryInfo>  #include <QLibraryInfo>
29  #include <QTranslator>  #include <QTranslator>
30  #include <QLocale>  #include <QLocale>
31    
32  #if defined(__APPLE__)  // Toshi Nagata 20080105  #if defined(__APPLE__)  // Toshi Nagata 20080105
33  #include <QDir>  #include <QDir>
34  #endif  #endif
35    
36  #if QT_VERSION < 0x040300  #if WIN32
37  #define lighter(x)      light(x)  #define RELATIVE_LOCALE_DIR "/share/locale"
38  #define darker(x)       dark(x)  #elif defined(__APPLE__)
39    #define RELATIVE_LOCALE_DIR "/../Resources"
40  #endif  #endif
41    
42    
# Line 42  Line 44 
44  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
45  //  //
46    
47    #if QT_VERSION < 0x050000
48  #if defined(Q_WS_X11)  #if defined(Q_WS_X11)
49    #define CONFIG_X11
50    #endif
51    #else
52    #if defined(QT_X11EXTRAS_LIB)
53    #define CONFIG_X11
54    #endif
55    #endif
56    
57    
58    #ifdef CONFIG_X11
59    #ifdef CONFIG_XUNIQUE
60    
61  #include <QX11Info>  #include <QX11Info>
62    
63  #include <X11/Xatom.h>  #include <X11/Xatom.h>
64  #include <X11/Xlib.h>  #include <X11/Xlib.h>
65    
66  #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
67    
68    #if QT_VERSION >= 0x050100
69    
70    #include <xcb/xcb.h>
71    #include <xcb/xproto.h>
72    
73    #include <QAbstractNativeEventFilter>
74    
75    class qsamplerApplication;
76    
77    class qsamplerXcbEventFilter : public QAbstractNativeEventFilter
78    {
79    public:
80    
81            // Constructor.
82            qsamplerXcbEventFilter(qsamplerApplication *pApp)
83                    : QAbstractNativeEventFilter(), m_pApp(pApp) {}
84    
85            // XCB event filter (virtual processor).
86            bool nativeEventFilter(const QByteArray& eventType, void *message, long *);
87    
88    private:
89    
90            // Instance variable.
91            qsamplerApplication *m_pApp;
92    };
93    
94  #endif  #endif
95    
96    #endif  // CONFIG_XUNIQUE
97    #endif  // CONFIG_X11
98    
99    
100  class qsamplerApplication : public QApplication  class qsamplerApplication : public QApplication
101  {  {
102  public:  public:
103    
104          // Constructor.          // Constructor.
105          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
106                  m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)                      m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
107          {          {
108                  // Load translation support.                  // Load translation support.
109                  QLocale loc;                  QLocale loc;
# Line 71  public: Line 115  public:
115                          if (m_pQtTranslator->load(sLocName, sLocPath)) {                          if (m_pQtTranslator->load(sLocName, sLocPath)) {
116                                  QApplication::installTranslator(m_pQtTranslator);                                  QApplication::installTranslator(m_pQtTranslator);
117                          } else {                          } else {
118                  #if WIN32                          #ifdef RELATIVE_LOCALE_DIR
119                                  sLocPath = QApplication::applicationDirPath() + "/share/locale";                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
120                                  if (m_pQtTranslator->load(sLocName, sLocPath)) {                                  if (m_pQtTranslator->load(sLocName, sLocPath)) {
121                                          QApplication::installTranslator(m_pQtTranslator);                                          QApplication::installTranslator(m_pQtTranslator);
122                                  } else {                                  } else {
123                  #endif                          #endif
124                                  delete m_pQtTranslator;                                  delete m_pQtTranslator;
125                                  m_pQtTranslator = 0;                                  m_pQtTranslator = 0;
126                  #ifdef CONFIG_DEBUG                          #ifdef CONFIG_DEBUG
127                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
128                                          loc.name().toUtf8().constData(),                                          loc.name().toUtf8().constData(),
129                                          sLocPath.toUtf8().constData(),                                          sLocPath.toUtf8().constData(),
130                                          sLocName.toUtf8().constData());                                          sLocName.toUtf8().constData());
131                  #endif                          #endif
132                  #if WIN32                          #ifdef RELATIVE_LOCALE_DIR
133                                  }                                  }
134                  #endif                          #endif
135                          }                          }
136                          // Try own application translation...                          // Try own application translation...
137                          m_pMyTranslator = new QTranslator(this);                          m_pMyTranslator = new QTranslator(this);
# Line 95  public: Line 139  public:
139                          if (m_pMyTranslator->load(sLocName, sLocPath)) {                          if (m_pMyTranslator->load(sLocName, sLocPath)) {
140                                  QApplication::installTranslator(m_pMyTranslator);                                  QApplication::installTranslator(m_pMyTranslator);
141                          } else {                          } else {
142                  #if WIN32                          #ifdef RELATIVE_LOCALE_DIR
143                                  sLocPath = QApplication::applicationDirPath() + "/share/locale";                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
144                  #else                          #else
145                                  sLocPath = CONFIG_PREFIX "/share/locale";                                  sLocPath = CONFIG_DATADIR "/qsampler/translations";
146                  #endif                          #endif
147                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {
148                                          QApplication::installTranslator(m_pMyTranslator);                                          QApplication::installTranslator(m_pMyTranslator);
149                                  } else {                                  } else {
150                                          delete m_pMyTranslator;                                          delete m_pMyTranslator;
151                                          m_pMyTranslator = 0;                                          m_pMyTranslator = 0;
152                  #ifdef CONFIG_DEBUG                                  #ifdef CONFIG_DEBUG
153                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
154                                                  loc.name().toUtf8().constData(),                                                  loc.name().toUtf8().constData(),
155                                                  sLocPath.toUtf8().constData(),                                                  sLocPath.toUtf8().constData(),
156                                                  sLocName.toUtf8().constData());                                                  sLocName.toUtf8().constData());
157                  #endif                                  #endif
158                                  }                                  }
159                          }                          }
160                  }                  }
161          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
162            #ifdef CONFIG_XUNIQUE
163                    // Instance uniqueness initialization...
164                  m_pDisplay = QX11Info::display();                  m_pDisplay = QX11Info::display();
165                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
166                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
167                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
168                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
169            #if QT_VERSION >= 0x050100
170                    m_pXcbEventFilter = new qsamplerXcbEventFilter(this);
171                    installNativeEventFilter(m_pXcbEventFilter);
172          #endif          #endif
173            #endif  // CONFIG_XUNIQUE
174            #endif  // CONFIG_X11
175          }          }
176    
177          // Destructor.          // Destructor.
178          ~qsamplerApplication()          ~qsamplerApplication()
179          {          {
180            #ifdef CONFIG_X11
181            #ifdef CONFIG_XUNIQUE
182            #if QT_VERSION >= 0x050100
183                    removeNativeEventFilter(m_pXcbEventFilter);
184                    delete m_pXcbEventFilter;
185            #endif
186            #endif  // CONFIG_XUNIQUE
187            #endif  // CONFIG_X11
188                  if (m_pMyTranslator) delete m_pMyTranslator;                  if (m_pMyTranslator) delete m_pMyTranslator;
189                  if (m_pQtTranslator) delete m_pQtTranslator;                  if (m_pQtTranslator) delete m_pQtTranslator;
190          }          }
# Line 134  public: Line 193  public:
193          void setMainWidget(QWidget *pWidget)          void setMainWidget(QWidget *pWidget)
194          {          {
195                  m_pWidget = pWidget;                  m_pWidget = pWidget;
196          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
197            #ifdef CONFIG_XUNIQUE
198                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
199                  m_wOwner = m_pWidget->winId();                  m_wOwner = m_pWidget->winId();
200                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
201                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
202          #endif          #endif  // CONFIG_XUNIQUE
203            #endif  // CONFIG_X11
204          }          }
205    
206          QWidget *mainWidget() const { return m_pWidget; }          QWidget *mainWidget() const { return m_pWidget; }
# Line 148  public: Line 209  public:
209      // and raise its proper main widget...      // and raise its proper main widget...
210          bool setup()          bool setup()
211          {          {
212          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
213            #ifdef CONFIG_XUNIQUE
214                  if (m_wOwner != None) {                  if (m_wOwner != None) {
215                          // First, notify any freedesktop.org WM                          // First, notify any freedesktop.org WM
216                          // that we're about to show the main widget...                          // that we're about to show the main widget...
# Line 172  public: Line 234  public:
234                          XSync(m_pDisplay, false);                          XSync(m_pDisplay, false);
235                          XRaiseWindow(m_pDisplay, m_wOwner);                          XRaiseWindow(m_pDisplay, m_wOwner);
236                          // And then, let it get caught on destination                          // And then, let it get caught on destination
237                          // by QApplication::x11EventFilter...                          // by QApplication::native/x11EventFilter...
238                          QByteArray value = QSAMPLER_XUNIQUE;                          const QByteArray value = QSAMPLER_XUNIQUE;
239                          XChangeProperty(                          XChangeProperty(
240                                  m_pDisplay,                                  m_pDisplay,
241                                  m_wOwner,                                  m_wOwner,
# Line 185  public: Line 247  public:
247                          // Done.                          // Done.
248                          return true;                          return true;
249                  }                  }
250          #endif          #endif  // CONFIG_XUNIQUE
251            #endif  // CONFIG_X11
252                  return false;                  return false;
253          }          }
254    
255  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
256          bool x11EventFilter(XEvent *pEv)  #ifdef CONFIG_XUNIQUE
257            void x11PropertyNotify(Window w)
258          {          {
259                  if (m_pWidget && m_wOwner != None                  if (m_pWidget && m_wOwner == w) {
                         && pEv->type == PropertyNotify  
                         && pEv->xproperty.window == m_wOwner  
                         && pEv->xproperty.state == PropertyNewValue) {  
260                          // Always check whether our property-flag is still around...                          // Always check whether our property-flag is still around...
261                          Atom aType;                          Atom aType;
262                          int iFormat = 0;                          int iFormat = 0;
# Line 226  public: Line 287  public:
287                          if (iItems > 0 && pData)                          if (iItems > 0 && pData)
288                                  XFree(pData);                                  XFree(pData);
289                  }                  }
290            }
291    #if QT_VERSION < 0x050000
292            bool x11EventFilter(XEvent *pEv)
293            {
294                    if (pEv->type == PropertyNotify
295                            && pEv->xproperty.state == PropertyNewValue)
296                            x11PropertyNotify(pEv->xproperty.window);
297                  return QApplication::x11EventFilter(pEv);                  return QApplication::x11EventFilter(pEv);
298          }          }
299  #endif  #endif
300    #endif  // CONFIG_XUNIQUE
301    #endif  // CONFIG_X11
302                    
303  private:  private:
304    
# Line 239  private: Line 309  private:
309          // Instance variables.          // Instance variables.
310          QWidget *m_pWidget;          QWidget *m_pWidget;
311    
312  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
313    #ifdef CONFIG_XUNIQUE
314          Display *m_pDisplay;          Display *m_pDisplay;
315          Atom     m_aUnique;          Atom     m_aUnique;
316          Window   m_wOwner;          Window   m_wOwner;
317    #if QT_VERSION >= 0x050100
318            qsamplerXcbEventFilter *m_pXcbEventFilter;
319  #endif  #endif
320    #endif  // CONFIG_XUNIQUE
321    #endif  // CONFIG_X11
322  };  };
323    
324    
325    #ifdef CONFIG_X11
326    #ifdef CONFIG_XUNIQUE
327    #if QT_VERSION >= 0x050100
328    // XCB Event filter (virtual processor).
329    bool qsamplerXcbEventFilter::nativeEventFilter (
330            const QByteArray& eventType, void *message, long * )
331    {
332            if (eventType == "xcb_generic_event_t") {
333                    xcb_property_notify_event_t *pEv
334                            = static_cast<xcb_property_notify_event_t *> (message);
335                    if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY
336                            && pEv->state == XCB_PROPERTY_NEW_VALUE)
337                            m_pApp->x11PropertyNotify(pEv->window);
338            }
339            return false;
340    }
341    #endif
342    #endif  // CONFIG_XUNIQUE
343    #endif  // CONFIG_X11
344    
345    
346  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
347  // stacktrace - Signal crash handler.  // stacktrace - Signal crash handler.
348  //  //
# Line 257  private: Line 353  private:
353  #include <stdio.h>  #include <stdio.h>
354  #include <errno.h>  #include <errno.h>
355  #include <signal.h>  #include <signal.h>
356    #include <unistd.h>
357  #include <sys/wait.h>  #include <sys/wait.h>
358    
359  void stacktrace ( int signo )  void stacktrace ( int signo )
# Line 354  int main ( int argc, char **argv ) Line 451  int main ( int argc, char **argv )
451          // Dark themes grayed/disabled color group fix...          // Dark themes grayed/disabled color group fix...
452          QPalette pal(app.palette());          QPalette pal(app.palette());
453          if (pal.base().color().value() < 0x7f) {          if (pal.base().color().value() < 0x7f) {
454            #if QT_VERSION >= 0x050000
455                    const QColor& color = pal.window().color();
456                    const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;
457                    for (int i = 0; i < iGroups; ++i) {
458                            const QPalette::ColorGroup group = QPalette::ColorGroup(i);
459                            pal.setBrush(group, QPalette::Light,    color.lighter(150));
460                            pal.setBrush(group, QPalette::Midlight, color.lighter(120));
461                            pal.setBrush(group, QPalette::Dark,     color.darker(150));
462                            pal.setBrush(group, QPalette::Mid,      color.darker(120));
463                            pal.setBrush(group, QPalette::Shadow,   color.darker(200));
464                    }
465            //      pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());
466            #endif
467                  pal.setColorGroup(QPalette::Disabled,                  pal.setColorGroup(QPalette::Disabled,
468                          pal.windowText().color().darker(),                          pal.windowText().color().darker(),
469                          pal.button(),                          pal.button(),
# Line 368  int main ( int argc, char **argv ) Line 478  int main ( int argc, char **argv )
478          }          }
479    
480          // Set default base font...          // Set default base font...
         int iBaseFontSize = app.font().pointSize();  
481          if (options.iBaseFontSize > 0)          if (options.iBaseFontSize > 0)
482                  iBaseFontSize = options.iBaseFontSize;                  app.setFont(QFont(app.font().family(), options.iBaseFontSize));
         app.setFont(QFont(app.font().family(), iBaseFontSize));  
483    
484          // Construct, setup and show the main form.          // Construct, setup and show the main form.
485          QSampler::MainForm w;          QSampler::MainForm w;

Legend:
Removed from v.2181  
changed lines
  Added in v.2987

  ViewVC Help
Powered by ViewVC