3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Angus Leeming
8 * Full author contact details are available in file CREDITS.
16 #include "qt_helpers.h"
19 #include "BufferView.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
25 #include "insets/Inset.h"
27 #include "support/debug.h"
28 #include "support/lassert.h"
40 using namespace lyx::support;
45 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
46 : name_(name), title_(title), lyxview_(&lv)
54 bool Dialog::canApply() const
56 FuncRequest const fr(getLfun(), fromqstr(name_));
57 FuncStatus const fs(getStatus(fr));
62 void Dialog::dispatch(FuncRequest const & fr) const
68 void Dialog::updateDialog() const
70 dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
74 void Dialog::disconnect() const
76 lyxview_->disconnectDialog(fromqstr(name_));
80 bool Dialog::isBufferAvailable() const
82 return lyxview_->currentBufferView() != 0;
86 bool Dialog::isBufferReadonly() const
88 if (!lyxview_->documentBufferView())
90 return lyxview_->documentBufferView()->buffer().isReadonly();
94 QString Dialog::bufferFilePath() const
96 return toqstr(buffer().filePath());
100 KernelDocType Dialog::docType() const
102 if (buffer().isLatex())
104 if (buffer().isLiterate())
111 BufferView const * Dialog::bufferview() const
113 return lyxview_->currentBufferView();
117 Buffer const & Dialog::buffer() const
119 LASSERT(lyxview_->currentBufferView(), /**/);
120 return lyxview_->currentBufferView()->buffer();
124 void Dialog::showData(string const & data)
126 if (isBufferDependent() && !isBufferAvailable())
129 if (!initialiseParams(data)) {
130 LYXERR0("Dialog \"" << name()
131 << "\" failed to translate the data string passed to show()");
141 if (isBufferDependent()) {
142 if (!isBufferAvailable() ||
143 (isBufferReadonly() && !canApplyToReadOnly()))
150 if (disconnectOnApply() && !isClosing()) {
152 initialiseParams(string());
158 void Dialog::prepareView()
160 // Make sure the dialog controls are correctly enabled/disabled with
164 QWidget * w = asQWidget();
165 w->setWindowTitle(title_);
167 QSize const hint = w->sizeHint();
168 if (hint.height() >= 0 && hint.width() >= 0)
169 w->setMinimumSize(hint);
173 void Dialog::showView()
177 QWidget * w = asQWidget();
178 if (w->isVisible()) {
184 if (wantInitialFocus())
188 lyxview_->activateWindow();
189 lyxview_->setFocus();
194 void Dialog::hideView()
196 QWidget * w = asQWidget();
205 bool Dialog::isVisibleView() const
207 return asQWidget()->isVisible();
211 Inset const * Dialog::inset(InsetCode code) const
213 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
215 ins = bufferview()->cursor().nextInset();
216 if (!ins || ins->lyxCode() != code)
222 void Dialog::checkStatus()
224 // buffer independent dialogs are always active.
225 // This check allows us leave canApply unimplemented for some dialogs.
226 if (!isBufferDependent()) {
231 // deactivate the dialog if we have no buffer
232 if (!isBufferAvailable()) {
237 // check whether this dialog may be active
239 bool const readonly = isBufferReadonly();
240 enableView(!readonly || canApplyToReadOnly());
241 // refreshReadOnly() is too generous in _enabling_ widgets
242 // update dialog to disable disabled widgets again
250 QString Dialog::sessionKey() const
252 return "views/" + QString::number(lyxview_->id())
257 void Dialog::saveSession() const
260 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
264 void Dialog::restoreSession()
267 asQWidget()->restoreGeometry(
268 settings.value(sessionKey() + "/geometry").toByteArray());
271 } // namespace frontend