/[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 2341 by capela, Sun Apr 8 19:40:42 2012 UTC revision 3493 by capela, Sat Mar 9 21:47:46 2019 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-2019, 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 20  Line 20 
20    
21  *****************************************************************************/  *****************************************************************************/
22    
23  #include "qsamplerAbout.h"  #include "qsampler.h"
24    
25  #include "qsamplerOptions.h"  #include "qsamplerOptions.h"
26  #include "qsamplerMainForm.h"  #include "qsamplerMainForm.h"
27    
 #include <QApplication>  
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"
 #define darker(x)       dark(x)  
 #endif  
   
 #define CONFIG_QUOTE1(x) #x  
 #define CONFIG_QUOTED(x) CONFIG_QUOTE1(x)  
   
 #if defined(DATADIR)  
 #define CONFIG_DATADIR CONFIG_QUOTED(DATADIR)  
 #else  
 #define CONFIG_DATADIR CONFIG_PREFIX "/share"  
38  #endif  #endif
39    
40  #if defined(LOCALEDIR)  #ifndef CONFIG_DATADIR
41  #define CONFIG_LOCALEDIR CONFIG_QUOTED(LOCALEDIR)  #define CONFIG_DATADIR  CONFIG_PREFIX "/share"
 #else  
 #define CONFIG_LOCALEDIR CONFIG_DATADIR "/locale"  
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 63  Line 52 
52  // Singleton application instance stuff (Qt/X11 only atm.)  // Singleton application instance stuff (Qt/X11 only atm.)
53  //  //
54    
55  #if defined(Q_WS_X11)  #ifdef CONFIG_X11
56    #ifdef CONFIG_XUNIQUE
57    
58  #include <QX11Info>  #include <unistd.h> /* for gethostname() */
59    
60  #include <X11/Xatom.h>  #include <X11/Xatom.h>
61  #include <X11/Xlib.h>  #include <X11/Xlib.h>
62    
63  #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"  #define QSAMPLER_XUNIQUE "qsamplerApplication"
64    
65  #endif  #if QT_VERSION >= 0x050100
66    
67  class qsamplerApplication : public QApplication  #include <xcb/xcb.h>
68    #include <xcb/xproto.h>
69    
70    #include <QAbstractNativeEventFilter>
71    
72    class qsamplerXcbEventFilter : public QAbstractNativeEventFilter
73  {  {
74  public:  public:
75    
76          // Constructor.          // Constructor.
77          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),          qsamplerXcbEventFilter(qsamplerApplication *pApp)
78                  m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)                      : QAbstractNativeEventFilter(), m_pApp(pApp) {}
79    
80            // XCB event filter (virtual processor).
81            bool nativeEventFilter(const QByteArray& eventType, void *message, long *)
82          {          {
83                  // Load translation support.                  if (eventType == "xcb_generic_event_t") {
84                  QLocale loc;                          xcb_property_notify_event_t *pEv
85                  if (loc.language() != QLocale::C) {                                  = static_cast<xcb_property_notify_event_t *> (message);
86                          // Try own Qt translation...                          if ((pEv->response_type & ~0x80) == XCB_PROPERTY_NOTIFY
87                          m_pQtTranslator = new QTranslator(this);                                  && pEv->state == XCB_PROPERTY_NEW_VALUE)
88                          QString sLocName = "qt_" + loc.name();                                  m_pApp->x11PropertyNotify(pEv->window);
89                          QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);                  }
90                    return false;
91            }
92    
93    private:
94    
95            // Instance variable.
96            qsamplerApplication *m_pApp;
97    };
98    
99    #endif
100    
101    #endif  // CONFIG_XUNIQUE
102    #endif  // CONFIG_X11
103    
104    
105    // Constructor.
106    qsamplerApplication::qsamplerApplication ( int& argc, char **argv )
107            : QApplication(argc, argv),
108                    m_pQtTranslator(NULL), m_pMyTranslator(NULL), m_pWidget(NULL)
109    {
110            // Load translation support.
111            QLocale loc;
112            if (loc.language() != QLocale::C) {
113                    // Try own Qt translation...
114                    m_pQtTranslator = new QTranslator(this);
115                    QString sLocName = "qt_" + loc.name();
116                    QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
117                    if (m_pQtTranslator->load(sLocName, sLocPath)) {
118                            QApplication::installTranslator(m_pQtTranslator);
119                    } else {
120                    #ifdef RELATIVE_LOCALE_DIR
121                            sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
122                          if (m_pQtTranslator->load(sLocName, sLocPath)) {                          if (m_pQtTranslator->load(sLocName, sLocPath)) {
123                                  QApplication::installTranslator(m_pQtTranslator);                                  QApplication::installTranslator(m_pQtTranslator);
124                          } else {                          } else {
                 #ifdef RELATIVE_LOCALE_DIR  
                                 sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;  
                                 if (m_pQtTranslator->load(sLocName, sLocPath)) {  
                                         QApplication::installTranslator(m_pQtTranslator);  
                                 } else {  
125                  #endif                  #endif
126                                  delete m_pQtTranslator;                          delete m_pQtTranslator;
127                                  m_pQtTranslator = 0;                          m_pQtTranslator = 0;
128                  #ifdef CONFIG_DEBUG                  #ifdef CONFIG_DEBUG
129                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
130                                          loc.name().toUtf8().constData(),                                  loc.name().toUtf8().constData(),
131                                          sLocPath.toUtf8().constData(),                                  sLocPath.toUtf8().constData(),
132                                          sLocName.toUtf8().constData());                                  sLocName.toUtf8().constData());
133                  #endif                  #endif
134                  #ifdef RELATIVE_LOCALE_DIR                  #ifdef RELATIVE_LOCALE_DIR
                                 }  
                 #endif  
135                          }                          }
136                          // Try own application translation...                  #endif
137                          m_pMyTranslator = new QTranslator(this);                  }
138                          sLocName = "qsampler_" + loc.name();                  // Try own application translation...
139                          if (m_pMyTranslator->load(sLocName, sLocPath)) {                  m_pMyTranslator = new QTranslator(this);
140                                  QApplication::installTranslator(m_pMyTranslator);                  sLocName = "qsampler_" + loc.name();
141                          } else {                  if (m_pMyTranslator->load(sLocName, sLocPath)) {
142                            QApplication::installTranslator(m_pMyTranslator);
143                    } else {
144                  #ifdef RELATIVE_LOCALE_DIR                  #ifdef RELATIVE_LOCALE_DIR
145                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;                          sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
146                  #else                  #else
147                                  sLocPath = CONFIG_LOCALEDIR;                          sLocPath = CONFIG_DATADIR "/qsampler/translations";
148                  #endif                  #endif
149                                  if (m_pMyTranslator->load(sLocName, sLocPath)) {                          if (m_pMyTranslator->load(sLocName, sLocPath)) {
150                                          QApplication::installTranslator(m_pMyTranslator);                                  QApplication::installTranslator(m_pMyTranslator);
151                                  } else {                          } else {
152                                          delete m_pMyTranslator;                                  delete m_pMyTranslator;
153                                          m_pMyTranslator = 0;                                  m_pMyTranslator = 0;
154                  #ifdef CONFIG_DEBUG                          #ifdef CONFIG_DEBUG
155                                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
156                                                  loc.name().toUtf8().constData(),                                          loc.name().toUtf8().constData(),
157                                                  sLocPath.toUtf8().constData(),                                          sLocPath.toUtf8().constData(),
158                                                  sLocName.toUtf8().constData());                                          sLocName.toUtf8().constData());
159                  #endif                          #endif
                                 }  
160                          }                          }
161                  }                  }
         #if defined(Q_WS_X11)  
                 m_pDisplay = QX11Info::display();  
                 m_aUnique  = XInternAtom(m_pDisplay, QSAMPLER_XUNIQUE, false);  
                 XGrabServer(m_pDisplay);  
                 m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);  
                 XUngrabServer(m_pDisplay);  
         #endif  
162          }          }
163    #ifdef CONFIG_X11
164    #ifdef CONFIG_XUNIQUE
165            m_pDisplay = NULL;
166            m_aUnique = 0;
167            m_wOwner = 0;
168    #if QT_VERSION >= 0x050100
169            m_pXcbEventFilter = new qsamplerXcbEventFilter(this);
170            installNativeEventFilter(m_pXcbEventFilter);
171    #endif
172    #endif  // CONFIG_XUNIQUE
173    #endif  // CONFIG_X11
174    }
175    
         // Destructor.  
         ~qsamplerApplication()  
         {  
                 if (m_pMyTranslator) delete m_pMyTranslator;  
                 if (m_pQtTranslator) delete m_pQtTranslator;  
         }  
176    
177          // Main application widget accessors.  // Destructor.
178          void setMainWidget(QWidget *pWidget)  qsamplerApplication::~qsamplerApplication (void)
179          {  {
180                  m_pWidget = pWidget;  #ifdef CONFIG_X11
181          #if defined(Q_WS_X11)  #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;
189            if (m_pQtTranslator) delete m_pQtTranslator;
190    }
191    
192    
193    // Main application widget accessors.
194    void qsamplerApplication::setMainWidget ( QWidget *pWidget )
195    {
196            m_pWidget = pWidget;
197    #ifdef CONFIG_X11
198    #ifdef CONFIG_XUNIQUE
199            m_wOwner = m_pWidget->winId();
200            if (m_pDisplay && m_wOwner) {
201                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
                 m_wOwner = m_pWidget->winId();  
202                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
203                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
         #endif  
204          }          }
205    #endif  // CONFIG_XUNIQUE
206    #endif  // CONFIG_X11
207    }
208    
         QWidget *mainWidget() const { return m_pWidget; }  
209    
210          // Check if another instance is running,  // Check if another instance is running,
211      // and raise its proper main widget...  // and raise its proper main widget...
212          bool setup()  bool qsamplerApplication::setup (void)
213          {  {
214          #if defined(Q_WS_X11)  #ifdef CONFIG_X11
215    #ifdef CONFIG_XUNIQUE
216    #if QT_VERSION >= 0x050100
217            if (!QX11Info::isPlatformX11())
218                    return false;
219    #endif
220            m_pDisplay = QX11Info::display();
221            if (m_pDisplay) {
222                    QString sUnique = QSAMPLER_XUNIQUE;
223                    char szHostName[255];
224                    if (::gethostname(szHostName, sizeof(szHostName)) == 0) {
225                            sUnique += '@';
226                            sUnique += szHostName;
227                    }
228                    m_aUnique = XInternAtom(m_pDisplay, sUnique.toUtf8().constData(), false);
229                    XGrabServer(m_pDisplay);
230                    m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
231                    XUngrabServer(m_pDisplay);
232                  if (m_wOwner != None) {                  if (m_wOwner != None) {
233                          // First, notify any freedesktop.org WM                          // First, notify any freedesktop.org WM
234                          // that we're about to show the main widget...                          // that we're about to show the main widget...
# Line 193  public: Line 252  public:
252                          XSync(m_pDisplay, false);                          XSync(m_pDisplay, false);
253                          XRaiseWindow(m_pDisplay, m_wOwner);                          XRaiseWindow(m_pDisplay, m_wOwner);
254                          // And then, let it get caught on destination                          // And then, let it get caught on destination
255                          // by QApplication::x11EventFilter...                          // by QApplication::native/x11EventFilter...
256                          QByteArray value = QSAMPLER_XUNIQUE;                          const QByteArray value = QSAMPLER_XUNIQUE;
257                          XChangeProperty(                          XChangeProperty(
258                                  m_pDisplay,                                  m_pDisplay,
259                                  m_wOwner,                                  m_wOwner,
# Line 206  public: Line 265  public:
265                          // Done.                          // Done.
266                          return true;                          return true;
267                  }                  }
         #endif  
                 return false;  
268          }          }
269    #endif  // CONFIG_XUNIQUE
270    #endif  // CONFIG_X11
271            return false;
272    }
273    
274  #if defined(Q_WS_X11)  
275          bool x11EventFilter(XEvent *pEv)  #ifdef CONFIG_X11
276          {  #ifdef CONFIG_XUNIQUE
277                  if (m_pWidget && m_wOwner != None  void qsamplerApplication::x11PropertyNotify ( Window w )
278                          && pEv->type == PropertyNotify  {
279                          && pEv->xproperty.window == m_wOwner          if (m_pDisplay && m_pWidget && m_wOwner == w) {
280                          && pEv->xproperty.state == PropertyNewValue) {                  // Always check whether our property-flag is still around...
281                          // Always check whether our property-flag is still around...                  Atom aType;
282                          Atom aType;                  int iFormat = 0;
283                          int iFormat = 0;                  unsigned long iItems = 0;
284                          unsigned long iItems = 0;                  unsigned long iAfter = 0;
285                          unsigned long iAfter = 0;                  unsigned char *pData = 0;
286                          unsigned char *pData = 0;                  if (XGetWindowProperty(
287                          if (XGetWindowProperty(                                  m_pDisplay,
288                                          m_pDisplay,                                  m_wOwner,
289                                          m_wOwner,                                  m_aUnique,
290                                          m_aUnique,                                  0, 1024,
291                                          0, 1024,                                  false,
292                                          false,                                  m_aUnique,
293                                          m_aUnique,                                  &aType,
294                                          &aType,                                  &iFormat,
295                                          &iFormat,                                  &iItems,
296                                          &iItems,                                  &iAfter,
297                                          &iAfter,                                  &pData) == Success
298                                          &pData) == Success                          && aType == m_aUnique && iItems > 0 && iAfter == 0) {
299                                  && aType == m_aUnique && iItems > 0 && iAfter == 0) {                          // Avoid repeating it-self...
300                                  // Avoid repeating it-self...                          XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
301                                  XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);                          // Just make it always shows up fine...
302                                  // Just make it always shows up fine...                          m_pWidget->show();
303                                  m_pWidget->show();                          m_pWidget->raise();
304                                  m_pWidget->raise();                          m_pWidget->activateWindow();
                                 m_pWidget->activateWindow();  
                         }  
                         // Free any left-overs...  
                         if (iItems > 0 && pData)  
                                 XFree(pData);  
305                  }                  }
306                  return QApplication::x11EventFilter(pEv);                  // Free any left-overs...
307                    if (iItems > 0 && pData)
308                            XFree(pData);
309          }          }
310  #endif  }
311            
 private:  
312    
313          // Translation support.  #if QT_VERSION < 0x050000
314          QTranslator *m_pQtTranslator;  bool qsamplerApplication::x11EventFilter ( XEvent *pEv )
315          QTranslator *m_pMyTranslator;  {
316            if (pEv->type == PropertyNotify
317          // Instance variables.                  && pEv->xproperty.state == PropertyNewValue)
318          QWidget *m_pWidget;                  x11PropertyNotify(pEv->xproperty.window);
319            return QApplication::x11EventFilter(pEv);
320  #if defined(Q_WS_X11)  }
         Display *m_pDisplay;  
         Atom     m_aUnique;  
         Window   m_wOwner;  
321  #endif  #endif
322  };  
323    #endif  // CONFIG_XUNIQUE
324    #endif  // CONFIG_X11
325    
326    
327  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
# Line 343  int main ( int argc, char **argv ) Line 399  int main ( int argc, char **argv )
399  #endif  #endif
400  #endif  #endif
401          qsamplerApplication app(argc, argv);          qsamplerApplication app(argc, argv);
402    #if QT_VERSION >= 0x050600
403            app.setAttribute(Qt::AA_EnableHighDpiScaling);
404    #endif
405          #if defined(__APPLE__)  //  Toshi Nagata 20080105          #if defined(__APPLE__)  //  Toshi Nagata 20080105
406          {          {
407                  //  Set the plugin path to @exetutable_path/../plugins                  //  Set the plugin path to @exetutable_path/../plugins
# Line 376  int main ( int argc, char **argv ) Line 434  int main ( int argc, char **argv )
434          // Dark themes grayed/disabled color group fix...          // Dark themes grayed/disabled color group fix...
435          QPalette pal(app.palette());          QPalette pal(app.palette());
436          if (pal.base().color().value() < 0x7f) {          if (pal.base().color().value() < 0x7f) {
437            #if QT_VERSION >= 0x050000
438                    const QColor& color = pal.window().color();
439                    const int iGroups = int(QPalette::Active | QPalette::Inactive) + 1;
440                    for (int i = 0; i < iGroups; ++i) {
441                            const QPalette::ColorGroup group = QPalette::ColorGroup(i);
442                            pal.setBrush(group, QPalette::Light,    color.lighter(150));
443                            pal.setBrush(group, QPalette::Midlight, color.lighter(120));
444                            pal.setBrush(group, QPalette::Dark,     color.darker(150));
445                            pal.setBrush(group, QPalette::Mid,      color.darker(120));
446                            pal.setBrush(group, QPalette::Shadow,   color.darker(200));
447                    }
448            //      pal.setColor(QPalette::Disabled, QPalette::ButtonText, pal.mid().color());
449            #endif
450                  pal.setColorGroup(QPalette::Disabled,                  pal.setColorGroup(QPalette::Disabled,
451                          pal.windowText().color().darker(),                          pal.windowText().color().darker(),
452                          pal.button(),                          pal.button(),
# Line 390  int main ( int argc, char **argv ) Line 461  int main ( int argc, char **argv )
461          }          }
462    
463          // Set default base font...          // Set default base font...
         int iBaseFontSize = app.font().pointSize();  
464          if (options.iBaseFontSize > 0)          if (options.iBaseFontSize > 0)
465                  iBaseFontSize = options.iBaseFontSize;                  app.setFont(QFont(app.font().family(), options.iBaseFontSize));
         app.setFont(QFont(app.font().family(), iBaseFontSize));  
466    
467          // Construct, setup and show the main form.          // Construct, setup and show the main form.
468          QSampler::MainForm w;          QSampler::MainForm w;
# Line 411  int main ( int argc, char **argv ) Line 480  int main ( int argc, char **argv )
480    
481    
482  // end of qsampler.cpp  // end of qsampler.cpp
483    

Legend:
Removed from v.2341  
changed lines
  Added in v.3493

  ViewVC Help
Powered by ViewVC