/[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 2210 by persson, Mon Jul 18 08:18:36 2011 UTC revision 3421 by capela, Sat Feb 17 19:45:17 2018 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-2018, rncbc aka Rui Nuno Capela. All rights reserved.
5     Copyright (C) 2007, 2008 Christian Schoenebeck     Copyright (C) 2007,2008,2015 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
8     modify it under the terms of the GNU General Public License     modify it under the terms of the GNU General Public License
# 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  #ifndef CONFIG_PREFIX
37  #define lighter(x)      light(x)  #define CONFIG_PREFIX   "/usr/local"
38  #define darker(x)       dark(x)  #endif
39    
40    #ifndef CONFIG_DATADIR
41    #define CONFIG_DATADIR  CONFIG_PREFIX "/share"
42  #endif  #endif
43    
44  #if WIN32  #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32)
45  #define RELATIVE_LOCALE_DIR "/share/locale"  #define RELATIVE_LOCALE_DIR "/share/locale"
46  #elif defined(__APPLE__)  #elif defined(__APPLE__)
47  #define RELATIVE_LOCALE_DIR "/../Resources"  #define RELATIVE_LOCALE_DIR "/../Resources"
# Line 48  Line 52 
52  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
53  //  //
54    
55    #if QT_VERSION < 0x050000
56  #if defined(Q_WS_X11)  #if defined(Q_WS_X11)
57    #define CONFIG_X11
58    #endif
59    #else
60    #if defined(QT_X11EXTRAS_LIB)
61    #define CONFIG_X11
62    #endif
63    #endif
64    
65    
66    #ifdef CONFIG_X11
67    #ifdef CONFIG_XUNIQUE
68    
69  #include <QX11Info>  #include <QX11Info>
70    
71  #include <X11/Xatom.h>  #include <X11/Xatom.h>
72  #include <X11/Xlib.h>  #include <X11/Xlib.h>
73    
74  #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
75    
76    #if QT_VERSION >= 0x050100
77    
78    #include <xcb/xcb.h>
79    #include <xcb/xproto.h>
80    
81    #include <QAbstractNativeEventFilter>
82    
83    class qsamplerApplication;
84    
85    class qsamplerXcbEventFilter : public QAbstractNativeEventFilter
86    {
87    public:
88    
89            // Constructor.
90            qsamplerXcbEventFilter(qsamplerApplication *pApp)
91                    : QAbstractNativeEventFilter(), m_pApp(pApp) {}
92    
93            // XCB event filter (virtual processor).
94            bool nativeEventFilter(const QByteArray& eventType, void *message, long *);
95    
96    private:
97    
98            // Instance variable.
99            qsamplerApplication *m_pApp;
100    };
101    
102  #endif  #endif
103    
104    #endif  // CONFIG_XUNIQUE
105    #endif  // CONFIG_X11
106    
107    
108  class qsamplerApplication : public QApplication  class qsamplerApplication : public QApplication
109  {  {
110  public:  public:
111    
112          // Constructor.          // Constructor.
113          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
114                  m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)                      m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
115          {          {
116                  // Load translation support.                  // Load translation support.
117                  QLocale loc;                  QLocale loc;
# Line 77  public: Line 123  public:
123                          if (m_pQtTranslator->load(sLocName, sLocPath)) {                          if (m_pQtTranslator->load(sLocName, sLocPath)) {
124                                  QApplication::installTranslator(m_pQtTranslator);                                  QApplication::installTranslator(m_pQtTranslator);
125                          } else {                          } else {
126                  #ifdef RELATIVE_LOCALE_DIR                          #ifdef RELATIVE_LOCALE_DIR
127                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
128                                  if (m_pQtTranslator->load(sLocName, sLocPath)) {                                  if (m_pQtTranslator->load(sLocName, sLocPath)) {
129                                          QApplication::installTranslator(m_pQtTranslator);                                          QApplication::installTranslator(m_pQtTranslator);
130                                  } else {                                  } else {
131                  #endif                          #endif
132                                  delete m_pQtTranslator;                                  delete m_pQtTranslator;
133                                  m_pQtTranslator = 0;                                  m_pQtTranslator = 0;
134                  #ifdef CONFIG_DEBUG                          #ifdef CONFIG_DEBUG
135                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
136                                          loc.name().toUtf8().constData(),                                          loc.name().toUtf8().constData(),
137                                          sLocPath.toUtf8().constData(),                                          sLocPath.toUtf8().constData(),
138                                          sLocName.toUtf8().constData());                                          sLocName.toUtf8().constData());
139                  #endif                          #endif
140                  #ifdef RELATIVE_LOCALE_DIR                          #ifdef RELATIVE_LOCALE_DIR
141                                  }                                  }
142                  #endif                          #endif
143                          }                          }
144                          // Try own application translation...                          // Try own application translation...
145                          m_pMyTranslator = new QTranslator(this);                          m_pMyTranslator = new QTranslator(this);
# Line 101  public: Line 147  public:
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                  #ifdef RELATIVE_LOCALE_DIR                          #ifdef RELATIVE_LOCALE_DIR
151                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
152                  #else                          #else
153                                  sLocPath = CONFIG_PREFIX "/share/locale";                                  sLocPath = CONFIG_DATADIR "/qsampler/translations";
154                  #endif                          #endif
155                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {
156                                          QApplication::installTranslator(m_pMyTranslator);                                          QApplication::installTranslator(m_pMyTranslator);
157                                  } else {                                  } else {
158                                          delete m_pMyTranslator;                                          delete m_pMyTranslator;
159                                          m_pMyTranslator = 0;                                          m_pMyTranslator = 0;
160                  #ifdef CONFIG_DEBUG                                  #ifdef CONFIG_DEBUG
161                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
162                                                  loc.name().toUtf8().constData(),                                                  loc.name().toUtf8().constData(),
163                                                  sLocPath.toUtf8().constData(),                                                  sLocPath.toUtf8().constData(),
164                                                  sLocName.toUtf8().constData());                                                  sLocName.toUtf8().constData());
165                  #endif                                  #endif
166                                  }                                  }
167                          }                          }
168                  }                  }
169          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
170                  m_pDisplay = QX11Info::display();          #ifdef CONFIG_XUNIQUE
171                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);                  m_pDisplay = NULL;
172                  XGrabServer(m_pDisplay);                  m_aUnique = 0;
173                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);                  m_wOwner = 0;
174                  XUngrabServer(m_pDisplay);          #if QT_VERSION >= 0x050100
175                    m_pXcbEventFilter = new qsamplerXcbEventFilter(this);
176                    installNativeEventFilter(m_pXcbEventFilter);
177                    if (QX11Info::isPlatformX11()) {
178            #endif
179                            // Instance uniqueness initialization...
180                            m_pDisplay = QX11Info::display();
181                            m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
182                            XGrabServer(m_pDisplay);
183                            m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
184                            XUngrabServer(m_pDisplay);
185            #if QT_VERSION >= 0x050100
186                    }
187          #endif          #endif
188            #endif  // CONFIG_XUNIQUE
189            #endif  // CONFIG_X11
190          }          }
191    
192          // Destructor.          // Destructor.
193          ~qsamplerApplication()          ~qsamplerApplication()
194          {          {
195            #ifdef CONFIG_X11
196            #ifdef CONFIG_XUNIQUE
197            #if QT_VERSION >= 0x050100
198                    removeNativeEventFilter(m_pXcbEventFilter);
199                    delete m_pXcbEventFilter;
200            #endif
201            #endif  // CONFIG_XUNIQUE
202            #endif  // CONFIG_X11
203                  if (m_pMyTranslator) delete m_pMyTranslator;                  if (m_pMyTranslator) delete m_pMyTranslator;
204                  if (m_pQtTranslator) delete m_pQtTranslator;                  if (m_pQtTranslator) delete m_pQtTranslator;
205          }          }
# Line 140  public: Line 208  public:
208          void setMainWidget(QWidget *pWidget)          void setMainWidget(QWidget *pWidget)
209          {          {
210                  m_pWidget = pWidget;                  m_pWidget = pWidget;
211          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
212                  XGrabServer(m_pDisplay);          #ifdef CONFIG_XUNIQUE
213                  m_wOwner = m_pWidget->winId();                  m_wOwner = m_pWidget->winId();
214                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  if (m_pDisplay && m_wOwner) {
215                  XUngrabServer(m_pDisplay);                          XGrabServer(m_pDisplay);
216          #endif                          XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
217                            XUngrabServer(m_pDisplay);
218                    }
219            #endif  // CONFIG_XUNIQUE
220            #endif  // CONFIG_X11
221          }          }
222    
223          QWidget *mainWidget() const { return m_pWidget; }          QWidget *mainWidget() const { return m_pWidget; }
# Line 154  public: Line 226  public:
226      // and raise its proper main widget...      // and raise its proper main widget...
227          bool setup()          bool setup()
228          {          {
229          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
230                  if (m_wOwner != None) {          #ifdef CONFIG_XUNIQUE
231                    if (m_pDisplay && m_wOwner != None) {
232                          // First, notify any freedesktop.org WM                          // First, notify any freedesktop.org WM
233                          // that we're about to show the main widget...                          // that we're about to show the main widget...
234                          Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);                          Screen *pScreen = XDefaultScreenOfDisplay(m_pDisplay);
# Line 178  public: Line 251  public:
251                          XSync(m_pDisplay, false);                          XSync(m_pDisplay, false);
252                          XRaiseWindow(m_pDisplay, m_wOwner);                          XRaiseWindow(m_pDisplay, m_wOwner);
253                          // And then, let it get caught on destination                          // And then, let it get caught on destination
254                          // by QApplication::x11EventFilter...                          // by QApplication::native/x11EventFilter...
255                          QByteArray value = QSAMPLER_XUNIQUE;                          const QByteArray value = QSAMPLER_XUNIQUE;
256                          XChangeProperty(                          XChangeProperty(
257                                  m_pDisplay,                                  m_pDisplay,
258                                  m_wOwner,                                  m_wOwner,
# Line 191  public: Line 264  public:
264                          // Done.                          // Done.
265                          return true;                          return true;
266                  }                  }
267          #endif          #endif  // CONFIG_XUNIQUE
268            #endif  // CONFIG_X11
269                  return false;                  return false;
270          }          }
271    
272  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
273          bool x11EventFilter(XEvent *pEv)  #ifdef CONFIG_XUNIQUE
274            void x11PropertyNotify(Window w)
275          {          {
276                  if (m_pWidget && m_wOwner != None                  if (m_pDisplay && m_pWidget && m_wOwner == w) {
                         && pEv->type == PropertyNotify  
                         && pEv->xproperty.window == m_wOwner  
                         && pEv->xproperty.state == PropertyNewValue) {  
277                          // Always check whether our property-flag is still around...                          // Always check whether our property-flag is still around...
278                          Atom aType;                          Atom aType;
279                          int iFormat = 0;                          int iFormat = 0;
# Line 232  public: Line 304  public:
304                          if (iItems > 0 && pData)                          if (iItems > 0 && pData)
305                                  XFree(pData);                                  XFree(pData);
306                  }                  }
307            }
308    #if QT_VERSION < 0x050000
309            bool x11EventFilter(XEvent *pEv)
310            {
311                    if (pEv->type == PropertyNotify
312                            && pEv->xproperty.state == PropertyNewValue)
313                            x11PropertyNotify(pEv->xproperty.window);
314                  return QApplication::x11EventFilter(pEv);                  return QApplication::x11EventFilter(pEv);
315          }          }
316  #endif  #endif
317    #endif  // CONFIG_XUNIQUE
318    #endif  // CONFIG_X11
319                    
320  private:  private:
321    
# Line 245  private: Line 326  private:
326          // Instance variables.          // Instance variables.
327          QWidget *m_pWidget;          QWidget *m_pWidget;
328    
329  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
330    #ifdef CONFIG_XUNIQUE
331          Display *m_pDisplay;          Display *m_pDisplay;
332          Atom     m_aUnique;          Atom     m_aUnique;
333          Window   m_wOwner;          Window   m_wOwner;
334    #if QT_VERSION >= 0x050100
335            qsamplerXcbEventFilter *m_pXcbEventFilter;
336  #endif  #endif
337    #endif  // CONFIG_XUNIQUE
338    #endif  // CONFIG_X11
339  };  };
340    
341    
342    #ifdef CONFIG_X11
343    #ifdef CONFIG_XUNIQUE
344    #if QT_VERSION >= 0x050100
345    // XCB Event filter (virtual processor).
346    bool qsamplerXcbEventFilter::nativeEventFilter (
347            const QByteArray& eventType, void *message, long * )
348    {
349            if (eventType == "xcb_generic_event_t") {
350                    xcb_property_notify_event_t *pEv
351                            = static_cast<xcb_property_notify_event_t *> (message);
352                    if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY
353                            && pEv->state == XCB_PROPERTY_NEW_VALUE)
354                            m_pApp->x11PropertyNotify(pEv->window);
355            }
356            return false;
357    }
358    #endif
359    #endif  // CONFIG_XUNIQUE
360    #endif  // CONFIG_X11
361    
362    
363  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
364  // stacktrace - Signal crash handler.  // stacktrace - Signal crash handler.
365  //  //
# Line 263  private: Line 370  private:
370  #include <stdio.h>  #include <stdio.h>
371  #include <errno.h>  #include <errno.h>
372  #include <signal.h>  #include <signal.h>
373    #include <unistd.h>
374  #include <sys/wait.h>  #include <sys/wait.h>
375    
376  void stacktrace ( int signo )  void stacktrace ( int signo )
# Line 360  int main ( int argc, char **argv ) Line 468  int main ( int argc, char **argv )
468          // Dark themes grayed/disabled color group fix...          // Dark themes grayed/disabled color group fix...
469          QPalette pal(app.palette());          QPalette pal(app.palette());
470          if (pal.base().color().value() < 0x7f) {          if (pal.base().color().value() < 0x7f) {
471            #if QT_VERSION >= 0x050000
472                    const QColor& color = pal.window().color();
473                    const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;
474                    for (int i = 0; i < iGroups; ++i) {
475                            const QPalette::ColorGroup group = QPalette::ColorGroup(i);
476                            pal.setBrush(group, QPalette::Light,    color.lighter(150));
477                            pal.setBrush(group, QPalette::Midlight, color.lighter(120));
478                            pal.setBrush(group, QPalette::Dark,     color.darker(150));
479                            pal.setBrush(group, QPalette::Mid,      color.darker(120));
480                            pal.setBrush(group, QPalette::Shadow,   color.darker(200));
481                    }
482            //      pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());
483            #endif
484                  pal.setColorGroup(QPalette::Disabled,                  pal.setColorGroup(QPalette::Disabled,
485                          pal.windowText().color().darker(),                          pal.windowText().color().darker(),
486                          pal.button(),                          pal.button(),
# Line 374  int main ( int argc, char **argv ) Line 495  int main ( int argc, char **argv )
495          }          }
496    
497          // Set default base font...          // Set default base font...
         int iBaseFontSize = app.font().pointSize();  
498          if (options.iBaseFontSize > 0)          if (options.iBaseFontSize > 0)
499                  iBaseFontSize = options.iBaseFontSize;                  app.setFont(QFont(app.font().family(), options.iBaseFontSize));
         app.setFont(QFont(app.font().family(), iBaseFontSize));  
500    
501          // Construct, setup and show the main form.          // Construct, setup and show the main form.
502          QSampler::MainForm w;          QSampler::MainForm w;
# Line 395  int main ( int argc, char **argv ) Line 514  int main ( int argc, char **argv )
514    
515    
516  // end of qsampler.cpp  // end of qsampler.cpp
517    

Legend:
Removed from v.2210  
changed lines
  Added in v.3421

  ViewVC Help
Powered by ViewVC