]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Alert_pimpl.C
Add vertical spacer.
[lyx.git] / src / frontends / qt4 / Alert_pimpl.C
1 /**
2  * \file qt4/Alert_pimpl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "Alert_pimpl.h"
15 #include "Alert.h"
16
17 #include "ui/QAskForTextUi.h"
18 #include "qt_helpers.h"
19
20 #include "frontends/Application.h"
21
22 #include "gettext.h"
23
24 #include <QApplication>
25 #include <QMessageBox>
26 #include <QLabel>
27 #include <QLineEdit>
28 #include <QDialog>
29 #include <QInputDialog>
30
31 #include <algorithm>
32
33 using std::pair;
34 using std::make_pair;
35
36 namespace lyx {
37
38 using lyx::support::bformat;
39 using lyx::docstring;
40
41 namespace {
42
43 class MessageBox: public QMessageBox
44 {
45 public:
46         MessageBox(QWidget * parent = 0): QMessageBox(parent)
47         {
48                 setAttribute(Qt::WA_DeleteOnClose, true);
49                 setAttribute(Qt::WA_QuitOnClose, false);
50         }
51 };
52
53 } // anonymous namespace
54
55
56 int prompt_pimpl(docstring const & tit, docstring const & question,
57                  int default_button, int cancel_button,
58                  docstring const & b1, docstring const & b2, docstring const & b3)
59 {
60         docstring const title = bformat(_("LyX: %1$s"), tit);
61
62         MessageBox mb;
63
64         // For some reason, sometimes Qt uses an hourglass or watch cursor when
65         // displaying the alert. Hence, we ask for the standard cursor shape.
66         // This call has no effect if the cursor has not been overridden.
67         qApp->changeOverrideCursor(Qt::ArrowCursor);
68
69         // FIXME replace that with theApp->gui()->currentView()
70         int res = mb.information(qApp->focusWidget(),
71                                            toqstr(title),
72                                            toqstr(formatted(question)),
73                                            toqstr(b1),
74                                            toqstr(b2),
75                                            b3.empty() ? QString::null : toqstr(b3),
76                                            default_button, cancel_button);
77
78         // Qt bug: can return -1 on cancel or WM close, despite the docs.
79         if (res == -1)
80                 res = cancel_button;
81         return res;
82 }
83
84
85 void warning_pimpl(docstring const & tit, docstring const & message)
86 {
87         docstring const title = bformat(_("LyX: %1$s"), tit);
88
89         if (theApp() == 0) {
90                 int argc = 1;
91                 char * argv[1];
92                 QApplication app(argc, argv);
93                 QMessageBox::warning(0,
94                         toqstr(title),
95                         toqstr(formatted(message)));
96                 return;
97         }
98         MessageBox mb;
99         mb.warning(qApp->focusWidget(),
100                              toqstr(title),
101                              toqstr(formatted(message)));
102 }
103
104
105 void error_pimpl(docstring const & tit, docstring const & message)
106 {
107         docstring const title = bformat(_("LyX: %1$s"), tit);
108         if (theApp() == 0) {
109                 int argc = 1;
110                 char * argv[1];
111                 QApplication app(argc, argv);
112                 QMessageBox::critical(0,
113                         toqstr(title),
114                         toqstr(formatted(message)));
115                 return;
116         }
117         MessageBox mb;
118         mb.critical(qApp->focusWidget(),
119                               toqstr(title),
120                               toqstr(formatted(message)));
121 }
122
123
124 void information_pimpl(docstring const & tit, docstring const & message)
125 {
126         docstring const title = bformat(_("LyX: %1$s"), tit);
127         MessageBox mb;
128         mb.information(qApp->focusWidget(),
129                                  toqstr(title),
130                                  toqstr(formatted(message)));
131 }
132
133
134 pair<bool, docstring> const
135 askForText_pimpl(docstring const & msg, docstring const & dflt)
136 {
137         docstring const title = bformat(_("LyX: %1$s"), msg);
138
139         bool ok;
140         QString text = QInputDialog::getText(qApp->focusWidget(),
141                 toqstr(title),
142                 toqstr(lyx::char_type('&') + msg),
143                 QLineEdit::Normal,
144                 toqstr(dflt), &ok);
145
146         if (ok && !text.isEmpty())
147                 return make_pair<bool, docstring>(true, qstring_to_ucs4(text));
148         else
149                 return make_pair<bool, docstring>(false, docstring());
150 }
151
152
153 } // namespace lyx