]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/Dialog.cpp
If we are in a closeEvent, we don't want to close all buffers, because these may...
[lyx.git] / src / frontends / qt4 / Dialog.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 "Dialog.h"
14
15 #include "GuiView.h"
16 #include "qt_helpers.h"
17
18 #include "Buffer.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "LyXFunc.h"
24
25 #include "insets/Inset.h"
26
27 #include "support/debug.h"
28 #include "support/lassert.h"
29
30 #include <QSettings>
31 #include <QString>
32
33 #include <string>
34
35 using namespace std;
36 using namespace lyx::support;
37
38 namespace lyx {
39 namespace frontend {
40
41
42 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
43         : name_(name), title_(title), lyxview_(&lv)
44 {}
45
46
47 Dialog::~Dialog()
48 {}
49
50
51 bool Dialog::canApply() const
52 {
53         FuncRequest const fr(getLfun(), fromqstr(name_));
54         FuncStatus const fs(getStatus(fr));
55         return fs.enabled();
56 }
57
58
59 void Dialog::dispatch(FuncRequest const & fr) const
60 {
61         theLyXFunc().setLyXView(lyxview_);
62         lyx::dispatch(fr);
63 }
64
65
66 void Dialog::updateDialog() const
67 {
68         dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
69 }
70
71
72 void Dialog::disconnect() const
73 {
74         lyxview_->disconnectDialog(fromqstr(name_));
75 }
76
77
78 bool Dialog::isBufferAvailable() const
79 {
80         return lyxview_->buffer() != 0;
81 }
82
83
84 bool Dialog::isBufferReadonly() const
85 {
86         if (!lyxview_->buffer())
87                 return true;
88         return lyxview_->buffer()->isReadonly();
89 }
90
91
92 QString Dialog::bufferFilepath() const
93 {
94         return toqstr(buffer().filePath());
95 }
96
97
98 KernelDocType Dialog::docType() const
99 {
100         if (buffer().isLatex())
101                 return LATEX;
102         if (buffer().isLiterate())
103                 return LITERATE;
104
105         return DOCBOOK;
106 }
107
108
109 BufferView const * Dialog::bufferview() const
110 {
111         return lyxview_->view();
112 }
113
114
115 Buffer const & Dialog::buffer() const
116 {
117         LASSERT(lyxview_->buffer(), /**/);
118         return *lyxview_->buffer();
119 }
120
121
122 void Dialog::showData(string const & data)
123 {
124         if (isBufferDependent() && !isBufferAvailable())
125                 return;
126
127         if (!initialiseParams(data)) {
128                 LYXERR0("Dialog \"" << name()
129                         << "\" failed to translate the data string passed to show()");
130                 return;
131         }
132
133         showView();
134 }
135
136
137 void Dialog::apply()
138 {
139         if (isBufferDependent()) {
140                 if (!isBufferAvailable() ||
141                     (isBufferReadonly() && !canApplyToReadOnly()))
142                         return;
143         }
144
145         applyView();
146         dispatchParams();
147
148         if (disconnectOnApply() && !isClosing()) {
149                 disconnect();
150                 initialiseParams(string());
151                 updateView();
152         }
153 }
154
155
156 void Dialog::prepareView()
157 {
158         // Make sure the dialog controls are correctly enabled/disabled with
159         // readonly status.
160         checkStatus();
161
162         QWidget * w = asQWidget();
163         w->setWindowTitle(title_);
164
165         QSize const hint = w->sizeHint();
166         if (hint.height() >= 0 && hint.width() >= 0)
167                 w->setMinimumSize(hint);
168 }
169
170
171 void Dialog::showView()
172 {
173         prepareView();
174
175         QWidget * w = asQWidget();
176         if (w->isVisible()) {
177                 w->raise();
178                 w->activateWindow();
179         } else
180                 w->show();
181
182         if (wantInitialFocus())
183                 w->setFocus();
184         else {
185                 lyxview_->raise();
186                 lyxview_->activateWindow();
187                 lyxview_->setFocus();
188         }
189 }
190
191
192 void Dialog::hideView()
193 {
194         QWidget * w = asQWidget();
195         if (!w->isVisible())
196                 return;
197         clearParams();
198         disconnect();
199         w->hide();
200 }
201
202
203 bool Dialog::isVisibleView() const
204 {
205         return asQWidget()->isVisible();
206 }
207
208
209 Inset const * Dialog::inset(InsetCode code) const
210 {
211         Inset * ins = bufferview()->cursor().innerInsetOfType(code);
212         if (!ins)
213                 ins = bufferview()->cursor().nextInset();
214         if (!ins || ins->lyxCode() != code)
215                 return 0;
216         return ins;
217 }
218
219
220 void Dialog::checkStatus()
221 {
222         // buffer independant dialogs are always active.
223         // This check allows us leave canApply unimplemented for some dialogs.
224         if (!isBufferDependent()) {
225                 updateView();
226                 return;
227         }
228
229         // deactivate the dialog if we have no buffer
230         if (!isBufferAvailable()) {
231                 enableView(false);
232                 return;
233         }
234
235         // check whether this dialog may be active
236         if (canApply()) {
237                 bool const readonly = isBufferReadonly();
238                 enableView(!readonly || canApplyToReadOnly());
239                 // refreshReadOnly() is too generous in _enabling_ widgets
240                 // update dialog to disable disabled widgets again
241
242                 if (!readonly || canApplyToReadOnly())
243                         updateView();
244
245         } else
246                 enableView(false);
247 }
248
249
250 QString Dialog::sessionKey() const
251 {
252         return "views/" + QString::number(lyxview_->id())
253                 + "/" + name();
254 }
255
256
257 void Dialog::saveSession() const
258 {
259         QSettings settings;
260         settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
261 }
262
263
264 void Dialog::restoreSession()
265 {
266         QSettings settings;
267         asQWidget()->restoreGeometry(
268                 settings.value(sessionKey() + "/geometry").toByteArray());
269 }
270
271 } // namespace frontend
272 } // namespace lyx