qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
gui.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2015-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include "gui.h"
22 #include "widget.h"
23 #include "src/nexus.h"
24 #include <QApplication>
25 #include <QCoreApplication>
26 #include <QDebug>
27 #include <QDialogButtonBox>
28 #include <QInputDialog>
29 #include <QLabel>
30 #include <QMessageBox>
31 #include <QPushButton>
32 #include <QThread>
33 #include <assert.h>
34 
46 GUI::GUI(QObject* parent)
47  : QObject(parent)
48 {
49  assert(QThread::currentThread() == qApp->thread());
50  assert(Nexus::getDesktopGUI());
51 }
52 
57 {
58  static GUI gui;
59  return gui;
60 }
61 
62 // Implementation of the public clean interface
63 
69 void GUI::setEnabled(bool state)
70 {
71  if (QThread::currentThread() == qApp->thread()) {
72  getInstance()._setEnabled(state);
73  } else {
74  QMetaObject::invokeMethod(&getInstance(), "_setEnabled", Qt::BlockingQueuedConnection,
75  Q_ARG(bool, state));
76  }
77 }
78 
85 void GUI::setWindowTitle(const QString& title)
86 {
87  if (QThread::currentThread() == qApp->thread()) {
89  } else {
90  QMetaObject::invokeMethod(&getInstance(), "_setWindowTitle", Qt::BlockingQueuedConnection,
91  Q_ARG(const QString&, title));
92  }
93 }
94 
102 {
103  if (QThread::currentThread() == qApp->thread()) {
105  } else {
106  QMetaObject::invokeMethod(&getInstance(), "themeReload", Qt::BlockingQueuedConnection);
107  }
108 }
109 
115 void GUI::showInfo(const QString& title, const QString& msg)
116 {
117  if (QThread::currentThread() == qApp->thread()) {
118  getInstance()._showInfo(title, msg);
119  } else {
120  QMetaObject::invokeMethod(&getInstance(), "_showInfo", Qt::BlockingQueuedConnection,
121  Q_ARG(const QString&, title), Q_ARG(const QString&, msg));
122  }
123 }
124 
130 void GUI::showWarning(const QString& title, const QString& msg)
131 {
132  if (QThread::currentThread() == qApp->thread()) {
133  getInstance()._showWarning(title, msg);
134  } else {
135  QMetaObject::invokeMethod(&getInstance(), "_showWarning", Qt::BlockingQueuedConnection,
136  Q_ARG(const QString&, title), Q_ARG(const QString&, msg));
137  }
138 }
139 
145 void GUI::showError(const QString& title, const QString& msg)
146 {
147  if (QThread::currentThread() == qApp->thread()) {
148  // If the GUI hasn't started yet and we're on the main thread,
149  // we still want to be able to show error messages
150  if (!Nexus::getDesktopGUI())
151  QMessageBox::critical(nullptr, title, msg);
152  else
153  getInstance()._showError(title, msg);
154  } else {
155  QMetaObject::invokeMethod(&getInstance(), "_showError", Qt::BlockingQueuedConnection,
156  Q_ARG(const QString&, title), Q_ARG(const QString&, msg));
157  }
158 }
159 
169 bool GUI::askQuestion(const QString& title, const QString& msg, bool defaultAns, bool warning, bool yesno)
170 {
171  if (QThread::currentThread() == qApp->thread()) {
172  return getInstance()._askQuestion(title, msg, defaultAns, warning, yesno);
173  } else {
174  bool ret;
175  QMetaObject::invokeMethod(&getInstance(), "_askQuestion", Qt::BlockingQueuedConnection,
176  Q_RETURN_ARG(bool, ret), Q_ARG(const QString&, title),
177  Q_ARG(const QString&, msg), Q_ARG(bool, defaultAns),
178  Q_ARG(bool, warning), Q_ARG(bool, yesno));
179  return ret;
180  }
181 }
182 
195 bool GUI::askQuestion(const QString& title, const QString& msg, const QString& button1,
196  const QString& button2, bool defaultAns, bool warning)
197 {
198  if (QThread::currentThread() == qApp->thread()) {
199  return getInstance()._askQuestion(title, msg, button1, button2, defaultAns, warning);
200  } else {
201  bool ret;
202  QMetaObject::invokeMethod(&getInstance(), "_askQuestion", Qt::BlockingQueuedConnection,
203  Q_RETURN_ARG(bool, ret), Q_ARG(const QString&, title),
204  Q_ARG(const QString&, msg), Q_ARG(bool, defaultAns),
205  Q_ARG(bool, warning));
206  return ret;
207  }
208 }
209 
210 // Private implementations
211 
212 void GUI::_setEnabled(bool state)
213 {
215  if (w)
216  w->setEnabled(state);
217 }
218 
219 void GUI::_setWindowTitle(const QString& title)
220 {
221  QWidget* w = getMainWidget();
222  if (!w)
223  return;
224  if (title.isEmpty())
225  w->setWindowTitle("qTox");
226  else
227  w->setWindowTitle(title + " - qTox");
228 }
229 
230 void GUI::_showInfo(const QString& title, const QString& msg)
231 {
232  QMessageBox messageBox(QMessageBox::Information, title, msg, QMessageBox::Ok, getMainWidget());
233  messageBox.setButtonText(QMessageBox::Ok, QApplication::tr("Ok"));
234  messageBox.exec();
235 }
236 
237 void GUI::_showWarning(const QString& title, const QString& msg)
238 {
239  QMessageBox messageBox(QMessageBox::Warning, title, msg, QMessageBox::Ok, getMainWidget());
240  messageBox.setButtonText(QMessageBox::Ok, QApplication::tr("Ok"));
241  messageBox.exec();
242 }
243 
244 void GUI::_showError(const QString& title, const QString& msg)
245 {
246  QMessageBox messageBox(QMessageBox::Critical, title, msg, QMessageBox::Ok, getMainWidget());
247  messageBox.setButtonText(QMessageBox::Ok, QApplication::tr("Ok"));
248  messageBox.exec();
249 }
250 
251 bool GUI::_askQuestion(const QString& title, const QString& msg, bool defaultAns, bool warning,
252  bool yesno)
253 {
254  QString positiveButton = yesno ? QApplication::tr("Yes") : QApplication::tr("Ok");
255  QString negativeButton = yesno ? QApplication::tr("No") : QApplication::tr("Cancel");
256 
257  return _askQuestion(title, msg, positiveButton, negativeButton, defaultAns, warning);
258 }
259 
260 bool GUI::_askQuestion(const QString& title, const QString& msg, const QString& button1,
261  const QString& button2, bool defaultAns, bool warning)
262 {
263  QMessageBox::Icon icon = warning ? QMessageBox::Warning : QMessageBox::Question;
264  QMessageBox box(icon, title, msg, QMessageBox::NoButton, getMainWidget());
265  QPushButton* pushButton1 = box.addButton(button1, QMessageBox::AcceptRole);
266  QPushButton* pushButton2 = box.addButton(button2, QMessageBox::RejectRole);
267  box.setDefaultButton(defaultAns ? pushButton1 : pushButton2);
268  box.setEscapeButton(pushButton2);
269 
270  box.exec();
271  return box.clickedButton() == pushButton1;
272 }
273 
274 // Other
275 
281 {
282  QWidget* maingui{nullptr};
283  maingui = Nexus::getDesktopGUI();
284  return maingui;
285 }
GUI::setEnabled
static void setEnabled(bool state)
Will enable or disable the GUI.
Definition: gui.cpp:69
GUI::_showWarning
void _showWarning(const QString &title, const QString &msg)
Definition: gui.cpp:237
GUI::askQuestion
static bool askQuestion(const QString &title, const QString &msg, bool defaultAns=false, bool warning=true, bool yesno=true)
Asks the user a question with Ok/Cancel or Yes/No buttons.
Definition: gui.cpp:169
GUI::_askQuestion
bool _askQuestion(const QString &title, const QString &msg, bool defaultAns=false, bool warning=true, bool yesno=true)
Definition: gui.cpp:251
GUI
Abstracts the GUI from the target backend (DesktopGUI, ...)
Definition: gui.h:27
GUI::setWindowTitle
static void setWindowTitle(const QString &title)
Change the title of the main window.
Definition: gui.cpp:85
GUI::getInstance
static GUI & getInstance()
Returns the singleton instance.
Definition: gui.cpp:56
GUI::_showError
void _showError(const QString &title, const QString &msg)
Definition: gui.cpp:244
GUI::showInfo
static void showInfo(const QString &title, const QString &msg)
Show some text to the user.
Definition: gui.cpp:115
widget.h
GUI::themeReload
void themeReload()
GUI::getMainWidget
static QWidget * getMainWidget()
Get the main widget.
Definition: gui.cpp:280
GUI::_showInfo
void _showInfo(const QString &title, const QString &msg)
Definition: gui.cpp:230
GUI::reloadTheme
static void reloadTheme()
Reloads the application theme and redraw the window.
Definition: gui.cpp:101
Nexus::getDesktopGUI
static Widget * getDesktopGUI()
Get desktop GUI widget.
Definition: nexus.cpp:340
GUI::showError
static void showError(const QString &title, const QString &msg)
Show an error to the user.
Definition: gui.cpp:145
GUI::showWarning
static void showWarning(const QString &title, const QString &msg)
Show a warning to the user.
Definition: gui.cpp:130
GUI::_setEnabled
void _setEnabled(bool state)
Definition: gui.cpp:212
gui.h
GUI::_setWindowTitle
void _setWindowTitle(const QString &title)
Definition: gui.cpp:219
nexus.h
Widget
Definition: widget.h:87
GUI::GUI
GUI(QObject *parent=nullptr)
Definition: gui.cpp:46