/[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 2074 by capela, Mon Mar 29 17:00:30 2010 UTC revision 2839 by capela, Tue Aug 25 18:36:55 2015 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-2015, 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  #define CONFIG_QUOTE1(x) #x
37  #define lighter(x)      light(x)  #define CONFIG_QUOTED(x) CONFIG_QUOTE1(x)
38  #define darker(x)       dark(x)  
39    #if defined(DATADIR)
40    #define CONFIG_DATADIR CONFIG_QUOTED(DATADIR)
41    #else
42    #define CONFIG_DATADIR CONFIG_PREFIX "/share"
43    #endif
44    
45    #if WIN32
46    #define RELATIVE_LOCALE_DIR "/share/locale"
47    #elif defined(__APPLE__)
48    #define RELATIVE_LOCALE_DIR "/../Resources"
49  #endif  #endif
50    
51    
# Line 42  Line 53 
53  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
54  //  //
55    
56    #if QT_VERSION < 0x050000
57  #if defined(Q_WS_X11)  #if defined(Q_WS_X11)
58    #define CONFIG_X11
59    #endif
60    #else
61    #if defined(QT_X11EXTRAS_LIB)
62    #define CONFIG_X11
63    #endif
64    #endif
65    
66    
67    #ifdef CONFIG_X11
68    #ifdef CONFIG_XUNIQUE
69    
70  #include <QX11Info>  #include <QX11Info>
71    
72  #include <X11/Xatom.h>  #include <X11/Xatom.h>
73  #include <X11/Xlib.h>  #include <X11/Xlib.h>
74    
75  #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
76    
77    #if QT_VERSION >= 0x050100
78    
79    #include <xcb/xcb.h>
80    #include <xcb/xproto.h>
81    
82    #include <QAbstractNativeEventFilter>
83    
84    class qsamplerApplication;
85    
86    class qsamplerXcbEventFilter : public QAbstractNativeEventFilter
87    {
88    public:
89    
90            // Constructor.
91            qsamplerXcbEventFilter(qsamplerApplication *pApp)
92                    : QAbstractNativeEventFilter(), m_pApp(pApp) {}
93    
94            // XCB event filter (virtual processor).
95            bool nativeEventFilter(const QByteArray& eventType, void *message, long *);
96    
97    private:
98    
99            // Instance variable.
100            qsamplerApplication *m_pApp;
101    };
102    
103  #endif  #endif
104    
105    #endif  // CONFIG_XUNIQUE
106    #endif  // CONFIG_X11
107    
108    
109  class qsamplerApplication : public QApplication  class qsamplerApplication : public QApplication
110  {  {
111  public:  public:
112    
113          // Constructor.          // Constructor.
114          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),
115                  m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)                      m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)
116          {          {
117                  // Load translation support.                  // Load translation support.
118                  QLocale loc;                  QLocale loc;
# Line 71  public: Line 124  public:
124                          if (m_pQtTranslator->load(sLocName, sLocPath)) {                          if (m_pQtTranslator->load(sLocName, sLocPath)) {
125                                  QApplication::installTranslator(m_pQtTranslator);                                  QApplication::installTranslator(m_pQtTranslator);
126                          } else {                          } else {
127                            #ifdef RELATIVE_LOCALE_DIR
128                                    sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
129                                    if (m_pQtTranslator->load(sLocName, sLocPath)) {
130                                            QApplication::installTranslator(m_pQtTranslator);
131                                    } else {
132                            #endif
133                                  delete m_pQtTranslator;                                  delete m_pQtTranslator;
134                                  m_pQtTranslator = 0;                                  m_pQtTranslator = 0;
135                  #ifdef CONFIG_DEBUG                          #ifdef CONFIG_DEBUG
136                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
137                                          loc.name().toUtf8().constData(),                                          loc.name().toUtf8().constData(),
138                                          sLocPath.toUtf8().constData(),                                          sLocPath.toUtf8().constData(),
139                                          sLocName.toUtf8().constData());                                          sLocName.toUtf8().constData());
140                  #endif                          #endif
141                            #ifdef RELATIVE_LOCALE_DIR
142                                    }
143                            #endif
144                          }                          }
145                          // Try own application translation...                          // Try own application translation...
146                          m_pMyTranslator = new QTranslator(this);                          m_pMyTranslator = new QTranslator(this);
# Line 86  public: Line 148  public:
148                          if (m_pMyTranslator->load(sLocName, sLocPath)) {                          if (m_pMyTranslator->load(sLocName, sLocPath)) {
149                                  QApplication::installTranslator(m_pMyTranslator);                                  QApplication::installTranslator(m_pMyTranslator);
150                          } else {                          } else {
151                                  sLocPath = CONFIG_PREFIX "/share/locale";                          #ifdef RELATIVE_LOCALE_DIR
152                                    sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
153                            #else
154                                    sLocPath = CONFIG_DATADIR "/qsampler/translations";
155                            #endif
156                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {
157                                          QApplication::installTranslator(m_pMyTranslator);                                          QApplication::installTranslator(m_pMyTranslator);
158                                  } else {                                  } else {
159                                          delete m_pMyTranslator;                                          delete m_pMyTranslator;
160                                          m_pMyTranslator = 0;                                          m_pMyTranslator = 0;
161                  #ifdef CONFIG_DEBUG                                  #ifdef CONFIG_DEBUG
162                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
163                                                  loc.name().toUtf8().constData(),                                                  loc.name().toUtf8().constData(),
164                                                  sLocPath.toUtf8().constData(),                                                  sLocPath.toUtf8().constData(),
165                                                  sLocName.toUtf8().constData());                                                  sLocName.toUtf8().constData());
166                  #endif                                  #endif
167                                  }                                  }
168                          }                          }
169                  }                  }
170          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
171            #ifdef CONFIG_XUNIQUE
172                    // Instance uniqueness initialization...
173                  m_pDisplay = QX11Info::display();                  m_pDisplay = QX11Info::display();
174                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);                  m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);
175                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
176                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);                  m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
177                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
178            #if QT_VERSION >= 0x050100
179                    m_pXcbEventFilter = new qsamplerXcbEventFilter(this);
180                    installNativeEventFilter(m_pXcbEventFilter);
181          #endif          #endif
182            #endif  // CONFIG_XUNIQUE
183            #endif  // CONFIG_X11
184          }          }
185    
186          // Destructor.          // Destructor.
187          ~qsamplerApplication()          ~qsamplerApplication()
188          {          {
189            #ifdef CONFIG_X11
190            #ifdef CONFIG_XUNIQUE
191            #if QT_VERSION >= 0x050100
192                    removeNativeEventFilter(m_pXcbEventFilter);
193                    delete m_pXcbEventFilter;
194            #endif
195            #endif  // CONFIG_XUNIQUE
196            #endif  // CONFIG_X11
197                  if (m_pMyTranslator) delete m_pMyTranslator;                  if (m_pMyTranslator) delete m_pMyTranslator;
198                  if (m_pQtTranslator) delete m_pQtTranslator;                  if (m_pQtTranslator) delete m_pQtTranslator;
199          }          }
# Line 121  public: Line 202  public:
202          void setMainWidget(QWidget *pWidget)          void setMainWidget(QWidget *pWidget)
203          {          {
204                  m_pWidget = pWidget;                  m_pWidget = pWidget;
205          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
206            #ifdef CONFIG_XUNIQUE
207                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
208                  m_wOwner = m_pWidget->winId();                  m_wOwner = m_pWidget->winId();
209                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
210                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
211          #endif          #endif  // CONFIG_XUNIQUE
212            #endif  // CONFIG_X11
213          }          }
214    
215          QWidget *mainWidget() const { return m_pWidget; }          QWidget *mainWidget() const { return m_pWidget; }
# Line 135  public: Line 218  public:
218      // and raise its proper main widget...      // and raise its proper main widget...
219          bool setup()          bool setup()
220          {          {
221          #if defined(Q_WS_X11)          #ifdef CONFIG_X11
222            #ifdef CONFIG_XUNIQUE
223                  if (m_wOwner != None) {                  if (m_wOwner != None) {
224                          // First, notify any freedesktop.org WM                          // First, notify any freedesktop.org WM
225                          // that we're about to show the main widget...                          // that we're about to show the main widget...
# Line 159  public: Line 243  public:
243                          XSync(m_pDisplay, false);                          XSync(m_pDisplay, false);
244                          XRaiseWindow(m_pDisplay, m_wOwner);                          XRaiseWindow(m_pDisplay, m_wOwner);
245                          // And then, let it get caught on destination                          // And then, let it get caught on destination
246                          // by QApplication::x11EventFilter...                          // by QApplication::native/x11EventFilter...
247                          QByteArray value = QSAMPLER_XUNIQUE;                          const QByteArray value = QSAMPLER_XUNIQUE;
248                          XChangeProperty(                          XChangeProperty(
249                                  m_pDisplay,                                  m_pDisplay,
250                                  m_wOwner,                                  m_wOwner,
# Line 172  public: Line 256  public:
256                          // Done.                          // Done.
257                          return true;                          return true;
258                  }                  }
259          #endif          #endif  // CONFIG_XUNIQUE
260            #endif  // CONFIG_X11
261                  return false;                  return false;
262          }          }
263    
264  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
265          bool x11EventFilter(XEvent *pEv)  #ifdef CONFIG_XUNIQUE
266            void x11PropertyNotify(Window w)
267          {          {
268                  if (m_pWidget && m_wOwner != None                  if (m_pWidget && m_wOwner == w) {
                         && pEv->type == PropertyNotify  
                         && pEv->xproperty.window == m_wOwner  
                         && pEv->xproperty.state == PropertyNewValue) {  
269                          // Always check whether our property-flag is still around...                          // Always check whether our property-flag is still around...
270                          Atom aType;                          Atom aType;
271                          int iFormat = 0;                          int iFormat = 0;
# Line 213  public: Line 296  public:
296                          if (iItems > 0 && pData)                          if (iItems > 0 && pData)
297                                  XFree(pData);                                  XFree(pData);
298                  }                  }
299            }
300    #if QT_VERSION < 0x050000
301            bool x11EventFilter(XEvent *pEv)
302            {
303                    if (pEv->type == PropertyNotify
304                            && pEv->xproperty.state == PropertyNewValue)
305                            x11PropertyNotify(pEv->xproperty.window);
306                  return QApplication::x11EventFilter(pEv);                  return QApplication::x11EventFilter(pEv);
307          }          }
308  #endif  #endif
309    #endif  // CONFIG_XUNIQUE
310    #endif  // CONFIG_X11
311                    
312  private:  private:
313    
# Line 226  private: Line 318  private:
318          // Instance variables.          // Instance variables.
319          QWidget *m_pWidget;          QWidget *m_pWidget;
320    
321  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
322    #ifdef CONFIG_XUNIQUE
323          Display *m_pDisplay;          Display *m_pDisplay;
324          Atom     m_aUnique;          Atom     m_aUnique;
325          Window   m_wOwner;          Window   m_wOwner;
326    #if QT_VERSION >= 0x050100
327            qsamplerXcbEventFilter *m_pXcbEventFilter;
328  #endif  #endif
329    #endif  // CONFIG_XUNIQUE
330    #endif  // CONFIG_X11
331  };  };
332    
333    
334    #ifdef CONFIG_X11
335    #ifdef CONFIG_XUNIQUE
336    #if QT_VERSION >= 0x050100
337    // XCB Event filter (virtual processor).
338    bool qsamplerXcbEventFilter::nativeEventFilter (
339            const QByteArray& eventType, void *message, long * )
340    {
341            if (eventType == "xcb_generic_event_t") {
342                    xcb_property_notify_event_t *pEv
343                            = static_cast<xcb_property_notify_event_t *> (message);
344                    if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY
345                            && pEv->state == XCB_PROPERTY_NEW_VALUE)
346                            m_pApp->x11PropertyNotify(pEv->window);
347            }
348            return false;
349    }
350    #endif
351    #endif  // CONFIG_XUNIQUE
352    #endif  // CONFIG_X11
353    
354    
355  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
356  // stacktrace - Signal crash handler.  // stacktrace - Signal crash handler.
357  //  //
# Line 244  private: Line 362  private:
362  #include <stdio.h>  #include <stdio.h>
363  #include <errno.h>  #include <errno.h>
364  #include <signal.h>  #include <signal.h>
365    #include <unistd.h>
366  #include <sys/wait.h>  #include <sys/wait.h>
367    
368  void stacktrace ( int signo )  void stacktrace ( int signo )
# Line 257  void stacktrace ( int signo ) Line 376  void stacktrace ( int signo )
376          signal(signo, SIG_DFL);          signal(signo, SIG_DFL);
377    
378          static const char *shell  = "/bin/sh";          static const char *shell  = "/bin/sh";
379          static const char *format = "gdb -q --batch --pid=%d --eval-command=bt";          static const char *format = "gdb -q --batch --pid=%d"
380                    " --eval-command='thread apply all bt'";
381    
382          snprintf(cmd, sizeof(cmd), format, (int) getpid());          snprintf(cmd, sizeof(cmd), format, (int) getpid());
383    
# Line 340  int main ( int argc, char **argv ) Line 460  int main ( int argc, char **argv )
460          // Dark themes grayed/disabled color group fix...          // Dark themes grayed/disabled color group fix...
461          QPalette pal(app.palette());          QPalette pal(app.palette());
462          if (pal.base().color().value() < 0x7f) {          if (pal.base().color().value() < 0x7f) {
463            #if QT_VERSION >= 0x050000
464                    const QColor& color = pal.window().color();
465                    const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;
466                    for (int i = 0; i < iGroups; ++i) {
467                            const QPalette::ColorGroup group = QPalette::ColorGroup(i);
468                            pal.setBrush(group, QPalette::Light,    color.lighter(150));
469                            pal.setBrush(group, QPalette::Midlight, color.lighter(120));
470                            pal.setBrush(group, QPalette::Dark,     color.darker(150));
471                            pal.setBrush(group, QPalette::Mid,      color.darker(120));
472                            pal.setBrush(group, QPalette::Shadow,   color.darker(200));
473                    }
474            //      pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());
475            #endif
476                  pal.setColorGroup(QPalette::Disabled,                  pal.setColorGroup(QPalette::Disabled,
477                          pal.windowText().color().darker(),                          pal.windowText().color().darker(),
478                          pal.button(),                          pal.button(),
# Line 354  int main ( int argc, char **argv ) Line 487  int main ( int argc, char **argv )
487          }          }
488    
489          // Set default base font...          // Set default base font...
         int iBaseFontSize = app.font().pointSize();  
490          if (options.iBaseFontSize > 0)          if (options.iBaseFontSize > 0)
491                  iBaseFontSize = options.iBaseFontSize;                  app.setFont(QFont(app.font().family(), options.iBaseFontSize));
         app.setFont(QFont(app.font().family(), iBaseFontSize));  
492    
493          // Construct, setup and show the main form.          // Construct, setup and show the main form.
494          QSampler::MainForm w;          QSampler::MainForm w;

Legend:
Removed from v.2074  
changed lines
  Added in v.2839

  ViewVC Help
Powered by ViewVC