]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Alert_pimpl.C
* LyXView::updateInset(): schedule a redraw instead of redraw immediately.
[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         // 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         MessageBox mb;
88         mb.warning(qApp->focusWidget(),
89                              toqstr(title),
90                              toqstr(formatted(message)));
91 }
92
93
94 void error_pimpl(docstring const & tit, docstring const & message)
95 {
96         docstring const title = bformat(_("LyX: %1$s"), tit);
97         MessageBox mb;
98         mb.critical(qApp->focusWidget(),
99                               toqstr(title),
100                               toqstr(formatted(message)));
101 }
102
103
104 void information_pimpl(docstring const & tit, docstring const & message)
105 {
106         docstring const title = bformat(_("LyX: %1$s"), tit);
107         MessageBox mb;
108         mb.information(qApp->focusWidget(),
109                                  toqstr(title),
110                                  toqstr(formatted(message)));
111 }
112
113
114 pair<bool, docstring> const
115 askForText_pimpl(docstring const & msg, docstring const & dflt)
116 {
117         docstring const title = bformat(_("LyX: %1$s"), msg);
118
119         bool ok;
120         QString text = QInputDialog::getText(qApp->focusWidget(),
121                 toqstr(title),
122                 toqstr(lyx::char_type('&') + msg),
123                 QLineEdit::Normal,
124                 toqstr(dflt), &ok);
125
126         if (ok && !text.isEmpty())
127                 return make_pair<bool, docstring>(true, qstring_to_ucs4(text));
128         else
129                 return make_pair<bool, docstring>(false, docstring());
130 }
131
132
133 } // namespace lyx