]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Alert_pimpl.C
* Painter.h:
[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 "gettext.h"
21
22 #include <QApplication>
23 #include <QMessageBox>
24 #include <QLabel>
25 #include <QLineEdit>
26 #include <QDialog>
27 #include <QInputDialog>
28
29 #include <algorithm>
30
31 using std::pair;
32 using std::make_pair;
33
34 namespace lyx {
35
36 using lyx::support::bformat;
37 using lyx::docstring;
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         // FIXME replace that with theApp->gui()->currentView()
63         int res = mb.information(qApp->focusWidget(),
64                                            toqstr(title),
65                                            toqstr(formatted(question)),
66                                            toqstr(b1),
67                                            toqstr(b2),
68                                            b3.empty() ? QString::null : toqstr(b3),
69                                            default_button, cancel_button);
70
71         // Qt bug: can return -1 on cancel or WM close, despite the docs.
72         if (res == -1)
73                 res = cancel_button;
74         return res;
75 }
76
77
78 void warning_pimpl(docstring const & tit, docstring const & message)
79 {
80         docstring const title = bformat(_("LyX: %1$s"), tit);
81
82         MessageBox mb;
83         mb.warning(qApp->focusWidget(),
84                              toqstr(title),
85                              toqstr(formatted(message)));
86 }
87
88
89 void error_pimpl(docstring const & tit, docstring const & message)
90 {
91         docstring const title = bformat(_("LyX: %1$s"), tit);
92         MessageBox mb;
93         mb.critical(qApp->focusWidget(),
94                               toqstr(title),
95                               toqstr(formatted(message)));
96 }
97
98
99 void information_pimpl(docstring const & tit, docstring const & message)
100 {
101         docstring const title = bformat(_("LyX: %1$s"), tit);
102         MessageBox mb;
103         mb.information(qApp->focusWidget(),
104                                  toqstr(title),
105                                  toqstr(formatted(message)));
106 }
107
108
109 pair<bool, docstring> const
110 askForText_pimpl(docstring const & msg, docstring const & dflt)
111 {
112         docstring const title = bformat(_("LyX: %1$s"), msg);
113
114         bool ok;
115         QString text = QInputDialog::getText(qApp->focusWidget(),
116                 toqstr(title),
117                 toqstr(lyx::char_type('&') + msg),
118                 QLineEdit::Normal,
119                 toqstr(dflt), &ok);
120
121         if (ok && !text.isEmpty())
122                 return make_pair<bool, docstring>(true, qstring_to_ucs4(text));
123         else
124                 return make_pair<bool, docstring>(false, docstring());
125 }
126
127
128 } // namespace lyx