]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/alert_pimpl.cpp
* src/frontends/qt4/ui/TextLayoutUi.ui:
[lyx.git] / src / frontends / qt4 / alert_pimpl.cpp
1 /**
2  * \file qt4/alert_pimpl.cpp
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/AskForTextUi.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 using lyx::support::bformat;
36
37 namespace lyx {
38
39 namespace {
40
41 class MessageBox: public QMessageBox
42 {
43 public:
44         MessageBox(QWidget * parent = 0) : QMessageBox(parent)
45         {
46                 setAttribute(Qt::WA_DeleteOnClose, true);
47                 setAttribute(Qt::WA_QuitOnClose, false);
48         }
49 };
50
51 } // anonymous namespace
52
53
54 int prompt_pimpl(docstring const & tit, docstring const & question,
55                  int default_button, int cancel_button,
56                  docstring const & b1, docstring const & b2, docstring const & b3)
57 {
58         docstring const title = bformat(_("LyX: %1$s"), tit);
59
60         MessageBox mb;
61
62         // For some reason, sometimes Qt uses an hourglass or watch cursor when
63         // displaying the alert. Hence, we ask for the standard cursor shape.
64         // This call has no effect if the cursor has not been overridden.
65         qApp->changeOverrideCursor(Qt::ArrowCursor);
66
67         // FIXME replace that with theApp->gui()->currentView()
68         int res = mb.information(qApp->focusWidget(),
69                                            toqstr(title),
70                                            toqstr(formatted(question)),
71                                            toqstr(b1),
72                                            toqstr(b2),
73                                            b3.empty() ? QString::null : toqstr(b3),
74                                            default_button, cancel_button);
75
76         // Qt bug: can return -1 on cancel or WM close, despite the docs.
77         if (res == -1)
78                 res = cancel_button;
79         return res;
80 }
81
82
83 void warning_pimpl(docstring const & tit, docstring const & message)
84 {
85         docstring const title = bformat(_("LyX: %1$s"), tit);
86
87         if (theApp() == 0) {
88                 int argc = 1;
89                 char * argv[1];
90                 QApplication app(argc, argv);
91                 QMessageBox::warning(0,
92                         toqstr(title),
93                         toqstr(formatted(message)));
94                 return;
95         }
96         MessageBox mb;
97         mb.warning(qApp->focusWidget(),
98                              toqstr(title),
99                              toqstr(formatted(message)));
100 }
101
102
103 void error_pimpl(docstring const & tit, docstring const & message)
104 {
105         docstring const title = bformat(_("LyX: %1$s"), tit);
106         if (theApp() == 0) {
107                 int argc = 1;
108                 char * argv[1];
109                 QApplication app(argc, argv);
110                 QMessageBox::critical(0,
111                         toqstr(title),
112                         toqstr(formatted(message)));
113                 return;
114         }
115         MessageBox mb;
116         mb.critical(qApp->focusWidget(),
117                               toqstr(title),
118                               toqstr(formatted(message)));
119 }
120
121
122 void information_pimpl(docstring const & tit, docstring const & message)
123 {
124         docstring const title = bformat(_("LyX: %1$s"), tit);
125         MessageBox mb;
126         mb.information(qApp->focusWidget(),
127                                  toqstr(title),
128                                  toqstr(formatted(message)));
129 }
130
131
132 pair<bool, docstring> const
133 askForText_pimpl(docstring const & msg, docstring const & dflt)
134 {
135         docstring const title = bformat(_("LyX: %1$s"), msg);
136
137         bool ok;
138         QString text = QInputDialog::getText(qApp->focusWidget(),
139                 toqstr(title),
140                 toqstr(char_type('&') + msg),
141                 QLineEdit::Normal,
142                 toqstr(dflt), &ok);
143
144         if (ok && !text.isEmpty())
145                 return make_pair<bool, docstring>(true, qstring_to_ucs4(text));
146         else
147                 return make_pair<bool, docstring>(false, docstring());
148 }
149
150
151 } // namespace lyx