/[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 2415 by capela, Thu Feb 14 14:57:43 2013 UTC revision 3496 by capela, Sun Mar 10 11:06:53 2019 UTC
# Line 1  Line 1 
1  // qsampler.cpp  // qsampler.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2004-2013, 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)  
38  #endif  #endif
39    
40  #define CONFIG_QUOTE1(x) #x  #ifndef CONFIG_DATADIR
41  #define CONFIG_QUOTED(x) CONFIG_QUOTE1(x)  #define CONFIG_DATADIR  CONFIG_PREFIX "/share"
   
 #if defined(DATADIR)  
 #define CONFIG_DATADIR CONFIG_QUOTED(DATADIR)  
 #else  
 #define CONFIG_DATADIR CONFIG_PREFIX "/share"  
 #endif  
   
 #if defined(LOCALEDIR)  
 #define CONFIG_LOCALEDIR CONFIG_QUOTED(LOCALEDIR)  
 #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_XUNIQUE
56    
57  #include <QX11Info>  #define QSAMPLER_XUNIQUE "qsamplerApplication"
58    
59    #if QT_VERSION < 0x050000
60    #ifdef CONFIG_X11
61    
62    #include <unistd.h> /* for gethostname() */
63    
64  #include <X11/Xatom.h>  #include <X11/Xatom.h>
65  #include <X11/Xlib.h>  #include <X11/Xlib.h>
66    
67  #define QSAMPLER_XUNIQUE "qsamplerMainForm_xunique"  #endif  // CONFIG_X11
68    #else
69    #include <QSharedMemory>
70    #include <QLocalServer>
71    #include <QLocalSocket>
72    #include <QHostInfo>
73  #endif  #endif
74    
75  class qsamplerApplication : public QApplication  #endif  // CONFIG_XUNIQUE
 {  
 public:  
76    
77          // Constructor.  
78          qsamplerApplication(int& argc, char **argv) : QApplication(argc, argv),  // Constructor.
79                  m_pQtTranslator(0), m_pMyTranslator(0), m_pWidget(0)      qsamplerApplication::qsamplerApplication ( int& argc, char **argv )
80          {          : QApplication(argc, argv),
81                  // Load translation support.                  m_pQtTranslator(NULL), m_pMyTranslator(NULL), m_pWidget(NULL)
82                  QLocale loc;  {
83                  if (loc.language() != QLocale::C) {          // Load translation support.
84                          // Try own Qt translation...          QLocale loc;
85                          m_pQtTranslator = new QTranslator(this);          if (loc.language() != QLocale::C) {
86                          QString sLocName = "qt_" + loc.name();                  // Try own Qt translation...
87                          QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);                  m_pQtTranslator = new QTranslator(this);
88                    QString sLocName = "qt_" + loc.name();
89                    QString sLocPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
90                    if (m_pQtTranslator->load(sLocName, sLocPath)) {
91                            QApplication::installTranslator(m_pQtTranslator);
92                    } else {
93                    #ifdef RELATIVE_LOCALE_DIR
94                            sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
95                          if (m_pQtTranslator->load(sLocName, sLocPath)) {                          if (m_pQtTranslator->load(sLocName, sLocPath)) {
96                                  QApplication::installTranslator(m_pQtTranslator);                                  QApplication::installTranslator(m_pQtTranslator);
97                          } else {                          } else {
                 #ifdef RELATIVE_LOCALE_DIR  
                                 sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;  
                                 if (m_pQtTranslator->load(sLocName, sLocPath)) {  
                                         QApplication::installTranslator(m_pQtTranslator);  
                                 } else {  
98                  #endif                  #endif
99                                  delete m_pQtTranslator;                          delete m_pQtTranslator;
100                                  m_pQtTranslator = 0;                          m_pQtTranslator = 0;
101                  #ifdef CONFIG_DEBUG                  #ifdef CONFIG_DEBUG
102                                  qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",                          qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
103                                          loc.name().toUtf8().constData(),                                  loc.name().toUtf8().constData(),
104                                          sLocPath.toUtf8().constData(),                                  sLocPath.toUtf8().constData(),
105                                          sLocName.toUtf8().constData());                                  sLocName.toUtf8().constData());
106                  #endif                  #endif
107                  #ifdef RELATIVE_LOCALE_DIR                  #ifdef RELATIVE_LOCALE_DIR
                                 }  
                 #endif  
108                          }                          }
109                          // Try own application translation...                  #endif
110                          m_pMyTranslator = new QTranslator(this);                  }
111                          sLocName = "qsampler_" + loc.name();                  // Try own application translation...
112                          if (m_pMyTranslator->load(sLocName, sLocPath)) {                  m_pMyTranslator = new QTranslator(this);
113                                  QApplication::installTranslator(m_pMyTranslator);                  sLocName = "qsampler_" + loc.name();
114                          } else {                  if (m_pMyTranslator->load(sLocName, sLocPath)) {
115                            QApplication::installTranslator(m_pMyTranslator);
116                    } else {
117                  #ifdef RELATIVE_LOCALE_DIR                  #ifdef RELATIVE_LOCALE_DIR
118                                  sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;                          sLocPath = QApplication::applicationDirPath() + RELATIVE_LOCALE_DIR;
119                  #else                  #else
120                                  sLocPath = CONFIG_LOCALEDIR;                          sLocPath = CONFIG_DATADIR "/qsampler/translations";
                 #endif  
                                 if (m_pMyTranslator->load(sLocName, sLocPath)) {  
                                         QApplication::installTranslator(m_pMyTranslator);  
                                 } else {  
                                         delete m_pMyTranslator;  
                                         m_pMyTranslator = 0;  
                 #ifdef CONFIG_DEBUG  
                                         qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",  
                                                 loc.name().toUtf8().constData(),  
                                                 sLocPath.toUtf8().constData(),  
                                                 sLocName.toUtf8().constData());  
121                  #endif                  #endif
122                                  }                          if (m_pMyTranslator->load(sLocName, sLocPath)) {
123                                    QApplication::installTranslator(m_pMyTranslator);
124                            } else {
125                                    delete m_pMyTranslator;
126                                    m_pMyTranslator = 0;
127                            #ifdef CONFIG_DEBUG
128                                    qWarning("Warning: no translation found for '%s' locale: %s/%s.qm",
129                                            loc.name().toUtf8().constData(),
130                                            sLocPath.toUtf8().constData(),
131                                            sLocName.toUtf8().constData());
132                            #endif
133                          }                          }
134                  }                  }
         #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  
135          }          }
136    #ifdef CONFIG_XUNIQUE
137    #if QT_VERSION < 0x050000
138    #ifdef CONFIG_X11
139            m_pDisplay = NULL;
140            m_aUnique = 0;
141            m_wOwner = 0;
142    #endif  // CONFIG_X11
143    #else
144            m_pMemory = NULL;
145            m_pServer = NULL;
146    #endif
147    #endif  // CONFIG_XUNIQUE
148    }
149    
150          // Destructor.  
151          ~qsamplerApplication()  // Destructor.
152          {  qsamplerApplication::~qsamplerApplication (void)
153                  if (m_pMyTranslator) delete m_pMyTranslator;  {
154                  if (m_pQtTranslator) delete m_pQtTranslator;  #ifdef CONFIG_XUNIQUE
155    #if QT_VERSION >= 0x050000
156            if (m_pServer) {
157                    m_pServer->close();
158                    delete m_pServer;
159                    m_pServer = NULL;
160          }          }
161            if (m_pMemory) {
162                    delete m_pMemory;
163                    m_pMemory = NULL;
164    }
165    #endif
166    #endif  // CONFIG_XUNIQUE
167            if (m_pMyTranslator) delete m_pMyTranslator;
168            if (m_pQtTranslator) delete m_pQtTranslator;
169    }
170    
171          // Main application widget accessors.  
172          void setMainWidget(QWidget *pWidget)  // Main application widget accessors.
173          {  void qsamplerApplication::setMainWidget ( QWidget *pWidget )
174                  m_pWidget = pWidget;  {
175          #if defined(Q_WS_X11)          m_pWidget = pWidget;
176    #ifdef CONFIG_XUNIQUE
177    #if QT_VERSION < 0x050000
178    #ifdef CONFIG_X11
179            m_wOwner = m_pWidget->winId();
180            if (m_pDisplay && m_wOwner) {
181                  XGrabServer(m_pDisplay);                  XGrabServer(m_pDisplay);
                 m_wOwner = m_pWidget->winId();  
182                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);                  XSetSelectionOwner(m_pDisplay, m_aUnique, m_wOwner, CurrentTime);
183                  XUngrabServer(m_pDisplay);                  XUngrabServer(m_pDisplay);
         #endif  
184          }          }
185    #endif  // CONFIG_X11
186    #endif
187    #endif  // CONFIG_XUNIQUE
188    }
189    
         QWidget *mainWidget() const { return m_pWidget; }  
190    
191          // Check if another instance is running,  // Check if another instance is running,
192      // and raise its proper main widget...  // and raise its proper main widget...
193          bool setup()  bool qsamplerApplication::setup (void)
194          {  {
195          #if defined(Q_WS_X11)  #ifdef CONFIG_XUNIQUE
196    #if QT_VERSION < 0x050000
197    #ifdef CONFIG_X11
198            m_pDisplay = QX11Info::display();
199            if (m_pDisplay) {
200                    QString sUnique = QSAMPLER_XUNIQUE;
201                    char szHostName[255];
202                    if (::gethostname(szHostName, sizeof(szHostName)) == 0) {
203                            sUnique += '@';
204                            sUnique += szHostName;
205                    }
206                    m_aUnique = XInternAtom(m_pDisplay, sUnique.toUtf8().constData(), false);
207                    XGrabServer(m_pDisplay);
208                    m_wOwner = XGetSelectionOwner(m_pDisplay, m_aUnique);
209                    XUngrabServer(m_pDisplay);
210                  if (m_wOwner != None) {                  if (m_wOwner != None) {
211                          // First, notify any freedesktop.org WM                          // First, notify any freedesktop.org WM
212                          // that we're about to show the main widget...                          // that we're about to show the main widget...
# Line 193  public: Line 230  public:
230                          XSync(m_pDisplay, false);                          XSync(m_pDisplay, false);
231                          XRaiseWindow(m_pDisplay, m_wOwner);                          XRaiseWindow(m_pDisplay, m_wOwner);
232                          // And then, let it get caught on destination                          // And then, let it get caught on destination
233                          // by QApplication::x11EventFilter...                          // by QApplication::native/x11EventFilter...
234                          QByteArray value = QSAMPLER_XUNIQUE;                          const QByteArray value = QSAMPLER_XUNIQUE;
235                          XChangeProperty(                          XChangeProperty(
236                                  m_pDisplay,                                  m_pDisplay,
237                                  m_wOwner,                                  m_wOwner,
# Line 206  public: Line 243  public:
243                          // Done.                          // Done.
244                          return true;                          return true;
245                  }                  }
         #endif  
                 return false;  
246          }          }
247    #endif  // CONFIG_X11
248  #if defined(Q_WS_X11)          return false;
249          bool x11EventFilter(XEvent *pEv)  #else
250          {          m_sUnique = QCoreApplication::applicationName();
251                  if (m_pWidget && m_wOwner != None          m_sUnique += '@';
252                          && pEv->type == PropertyNotify          m_sUnique += QHostInfo::localHostName();
253                          && pEv->xproperty.window == m_wOwner  #ifdef Q_OS_UNIX
254                          && pEv->xproperty.state == PropertyNewValue) {          m_pMemory = new QSharedMemory(m_sUnique);
255                          // Always check whether our property-flag is still around...          m_pMemory->attach();
256                          Atom aType;          delete m_pMemory;
257                          int iFormat = 0;  #endif
258                          unsigned long iItems = 0;          m_pMemory = new QSharedMemory(m_sUnique);
259                          unsigned long iAfter = 0;          bool bServer = false;
260                          unsigned char *pData = 0;          const qint64 pid = QCoreApplication::applicationPid();
261                          if (XGetWindowProperty(          struct Data { qint64 pid; };
262                                          m_pDisplay,          if (m_pMemory->create(sizeof(Data))) {
263                                          m_wOwner,                  m_pMemory->lock();
264                                          m_aUnique,                  Data *pData = static_cast<Data *> (m_pMemory->data());
265                                          0, 1024,                  if (pData) {
266                                          false,                          pData->pid = pid;
267                                          m_aUnique,                          bServer = true;
268                                          &aType,                  }
269                                          &iFormat,                  m_pMemory->unlock();
270                                          &iItems,          }
271                                          &iAfter,          else
272                                          &pData) == Success          if (m_pMemory->attach()) {
273                                  && aType == m_aUnique && iItems > 0 && iAfter == 0) {                  m_pMemory->lock(); // maybe not necessary?
274                                  // Avoid repeating it-self...                  Data *pData = static_cast<Data *> (m_pMemory->data());
275                                  XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);                  if (pData)
276                                  // Just make it always shows up fine...                          bServer = (pData->pid == pid);
277                                  m_pWidget->show();                  m_pMemory->unlock();
278                                  m_pWidget->raise();          }
279                                  m_pWidget->activateWindow();          if (bServer) {
280                          }                  QLocalServer::removeServer(m_sUnique);
281                          // Free any left-overs...                  m_pServer = new QLocalServer();
282                          if (iItems > 0 && pData)                  m_pServer->setSocketOptions(QLocalServer::UserAccessOption);
283                                  XFree(pData);                  m_pServer->listen(m_sUnique);
284                    QObject::connect(m_pServer,
285                            SIGNAL(newConnection()),
286                            SLOT(newConnectionSlot()));
287            } else {
288                    QLocalSocket socket;
289                    socket.connectToServer(m_sUnique);
290                    if (socket.state() == QLocalSocket::ConnectingState)
291                            socket.waitForConnected(200);
292                    if (socket.state() == QLocalSocket::ConnectedState) {
293                            socket.write(QCoreApplication::arguments().join(' ').toUtf8());
294                            socket.flush();
295                            socket.waitForBytesWritten(200);
296                  }                  }
                 return QApplication::x11EventFilter(pEv);  
297          }          }
298            return !bServer;
299  #endif  #endif
300            #else
301  private:          return false;
302    #endif  // !CONFIG_XUNIQUE
303    }
304    
305    
306    #ifdef CONFIG_XUNIQUE
307    #if QT_VERSION < 0x050000
308    #ifdef CONFIG_X11
309    
310          // Translation support.  void qsamplerApplication::x11PropertyNotify ( Window w )
311          QTranslator *m_pQtTranslator;  {
312          QTranslator *m_pMyTranslator;          if (m_pDisplay && m_pWidget && m_wOwner == w) {
313                    // Always check whether our property-flag is still around...
314                    Atom aType;
315                    int iFormat = 0;
316                    unsigned long iItems = 0;
317                    unsigned long iAfter = 0;
318                    unsigned char *pData = 0;
319                    if (XGetWindowProperty(
320                                    m_pDisplay,
321                                    m_wOwner,
322                                    m_aUnique,
323                                    0, 1024,
324                                    false,
325                                    m_aUnique,
326                                    &aType,
327                                    &iFormat,
328                                    &iItems,
329                                    &iAfter,
330                                    &pData) == Success
331                            && aType == m_aUnique && iItems > 0 && iAfter == 0) {
332                            // Avoid repeating it-self...
333                            XDeleteProperty(m_pDisplay, m_wOwner, m_aUnique);
334                            // Just make it always shows up fine...
335                            m_pWidget->show();
336                            m_pWidget->raise();
337                            m_pWidget->activateWindow();
338                    }
339                    // Free any left-overs...
340                    if (iItems > 0 && pData)
341                            XFree(pData);
342            }
343    }
344    
345    
346    bool qsamplerApplication::x11EventFilter ( XEvent *pEv )
347    {
348            if (pEv->type == PropertyNotify
349                    && pEv->xproperty.state == PropertyNewValue)
350                    x11PropertyNotify(pEv->xproperty.window);
351            return QApplication::x11EventFilter(pEv);
352    }
353    
354          // Instance variables.  #endif  // CONFIG_X11
355          QWidget *m_pWidget;  #else
356    
357    // Local server conection slot.
358    void qsamplerApplication::newConnectionSlot (void)
359    {
360            QLocalSocket *pSocket = m_pServer->nextPendingConnection();
361            QObject::connect(pSocket,
362                    SIGNAL(readyRead()),
363                    SLOT(readyReadSlot()));
364    }
365    
366    // Local server data-ready slot.
367    void qsamplerApplication::readyReadSlot (void)
368    {
369            QLocalSocket *pSocket = qobject_cast<QLocalSocket *> (sender());
370            if (pSocket) {
371                    const qint64 nread = pSocket->bytesAvailable();
372                    if (nread > 0) {
373                            QByteArray data = pSocket->read(nread);
374                            // Just make it always shows up fine...
375                            m_pWidget->hide();
376                            m_pWidget->show();
377                            m_pWidget->raise();
378                            m_pWidget->activateWindow();
379                    }
380            }
381    }
382    
 #if defined(Q_WS_X11)  
         Display *m_pDisplay;  
         Atom     m_aUnique;  
         Window   m_wOwner;  
383  #endif  #endif
384  };  #endif  // CONFIG_XUNIQUE
385    
386    
387  //-------------------------------------------------------------------------  //-------------------------------------------------------------------------
# Line 343  int main ( int argc, char **argv ) Line 459  int main ( int argc, char **argv )
459  #endif  #endif
460  #endif  #endif
461          qsamplerApplication app(argc, argv);          qsamplerApplication app(argc, argv);
462    #if QT_VERSION >= 0x050600
463            app.setAttribute(Qt::AA_EnableHighDpiScaling);
464    #endif
465          #if defined(__APPLE__)  //  Toshi Nagata 20080105          #if defined(__APPLE__)  //  Toshi Nagata 20080105
466          {          {
467                  //  Set the plugin path to @exetutable_path/../plugins                  //  Set the plugin path to @exetutable_path/../plugins
# Line 403  int main ( int argc, char **argv ) Line 521  int main ( int argc, char **argv )
521          }          }
522    
523          // Set default base font...          // Set default base font...
         int iBaseFontSize = app.font().pointSize();  
524          if (options.iBaseFontSize > 0)          if (options.iBaseFontSize > 0)
525                  iBaseFontSize = options.iBaseFontSize;                  app.setFont(QFont(app.font().family(), options.iBaseFontSize));
         app.setFont(QFont(app.font().family(), iBaseFontSize));  
526    
527          // Construct, setup and show the main form.          // Construct, setup and show the main form.
528          QSampler::MainForm w;          QSampler::MainForm w;
# Line 424  int main ( int argc, char **argv ) Line 540  int main ( int argc, char **argv )
540    
541    
542  // end of qsampler.cpp  // end of qsampler.cpp
543    

Legend:
Removed from v.2415  
changed lines
  Added in v.3496

  ViewVC Help
Powered by ViewVC