]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiDialog.cpp
Factorize closeEvent() for GuiDialog based dialogs.
[lyx.git] / src / frontends / qt4 / GuiDialog.cpp
1 /**
2  * \file Dialog.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "GuiDialog.h"
14 #include "GuiView.h"
15 #include "qt_helpers.h"
16
17 #include "support/debug.h"
18
19 #include <QCloseEvent>
20 #include <QMainWindow>
21 #include <QSettings>
22 #include <QShowEvent>
23
24 using namespace std;
25
26 namespace lyx {
27 namespace frontend {
28
29 GuiDialog::GuiDialog(GuiView & lv, string const & name, QString const & title)
30         :  QDialog(&lv), Dialog(lv, name, "LyX: " + title), is_closing_(false)
31 {}
32
33
34 void GuiDialog::closeEvent(QCloseEvent *e)
35 {
36         slotClose();
37         e->accept();
38 }
39
40
41 void GuiDialog::setButtonsValid(bool valid)
42 {
43         bc().setValid(valid);
44 }
45
46
47 void GuiDialog::slotApply()
48 {
49         apply();
50         bc().apply();
51 }
52
53
54 void GuiDialog::slotOK()
55 {
56         is_closing_ = true;
57         apply();
58         is_closing_ = false;
59         QDialog::hide();
60         bc().ok();
61 }
62
63
64 void GuiDialog::slotClose()
65 {
66         QDialog::hide();
67         bc().cancel();
68 }
69
70
71 void GuiDialog::slotRestore()
72 {
73         // Tell the controller that a request to refresh the dialog's contents
74         // has been received. It's up to the controller to supply the necessary
75         // info by calling GuiDialog::updateView().
76         updateDialog();
77         bc().restore();
78 }
79
80
81 void GuiDialog::changed()
82 {
83         if (updating_)
84                 return;
85         bc().setValid(isValid());
86 }
87
88
89 void GuiDialog::enableView(bool enable)
90 {
91         bc().setReadOnly(!enable);
92         bc().setValid(enable);
93         Dialog::enableView(enable);
94 }
95
96
97 void GuiDialog::updateView()
98 {
99         setUpdatesEnabled(false);
100
101         bc().setReadOnly(isBufferReadonly());
102         // protect the BC from unwarranted state transitions
103         updating_ = true;
104         updateContents();
105         updating_ = false;
106         // The widgets may not be valid, so refresh the button controller
107         bc().refresh();
108
109         setUpdatesEnabled(true);
110 }
111
112 } // namespace frontend
113 } // namespace lyx
114
115
116 /////////////////////////////////////////////////////////////////////
117 //
118 // Command based dialogs
119 //
120 /////////////////////////////////////////////////////////////////////
121
122 #include "FuncRequest.h"
123 #include "insets/InsetCommand.h"
124
125 using namespace std;
126
127 namespace lyx {
128 namespace frontend {
129
130 GuiCommand::GuiCommand(GuiView & lv, string const & name,
131         QString const & title)
132         : GuiDialog(lv, name, title), params_(insetCode(name)), lfun_name_(name)
133 {
134 }
135
136
137 bool GuiCommand::initialiseParams(string const & data)
138 {
139         // The name passed with LFUN_INSET_APPLY is also the name
140         // used to identify the mailer.
141         InsetCommandMailer::string2params(lfun_name_, data, params_);
142         return true;
143 }
144
145
146 void GuiCommand::dispatchParams()
147 {
148         if (lfun_name_.empty())
149                 return;
150
151         string const lfun = 
152                 InsetCommandMailer::params2string(lfun_name_, params_);
153         dispatch(FuncRequest(getLfun(), lfun));
154 }
155
156 } // namespace frontend
157 } // namespace lyx
158
159 #include "GuiDialog_moc.cpp"