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