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"
36 using namespace lyx::support;
42 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
43 : name_(name), title_(title), lyxview_(&lv)
51 bool Dialog::canApply() const
53 FuncRequest const fr(getLfun(), fromqstr(name_));
54 FuncStatus const fs(getStatus(fr));
59 void Dialog::dispatch(FuncRequest const & fr) const
61 theLyXFunc().setLyXView(lyxview_);
66 void Dialog::updateDialog() const
68 dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
72 void Dialog::disconnect() const
74 lyxview_->disconnectDialog(fromqstr(name_));
78 bool Dialog::isBufferAvailable() const
80 return lyxview_->buffer() != 0;
84 bool Dialog::isBufferReadonly() const
86 if (!lyxview_->buffer())
88 return lyxview_->buffer()->isReadonly();
92 QString Dialog::bufferFilepath() const
94 return toqstr(buffer().filePath());
98 KernelDocType Dialog::docType() const
100 if (buffer().isLatex())
102 if (buffer().isLiterate())
109 BufferView const * Dialog::bufferview() const
111 return lyxview_->view();
115 Buffer const & Dialog::buffer() const
117 LASSERT(lyxview_->buffer(), /**/);
118 return *lyxview_->buffer();
122 void Dialog::showData(string const & data)
124 if (isBufferDependent() && !isBufferAvailable())
127 if (!initialiseParams(data)) {
128 LYXERR0("Dialog \"" << name()
129 << "\" failed to translate the data string passed to show()");
139 if (isBufferDependent()) {
140 if (!isBufferAvailable() ||
141 (isBufferReadonly() && !canApplyToReadOnly()))
148 if (disconnectOnApply() && !isClosing()) {
150 initialiseParams(string());
156 void Dialog::showView()
158 // 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);
171 if (w->isVisible()) {
177 if (wantInitialFocus())
181 lyxview_->activateWindow();
182 lyxview_->setFocus();
187 void Dialog::hideView()
189 QWidget * w = asQWidget();
198 bool Dialog::isVisibleView() const
200 return asQWidget()->isVisible();
204 Inset const * Dialog::inset(InsetCode code) const
206 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
208 ins = bufferview()->cursor().nextInset();
209 if (!ins || ins->lyxCode() != code)
215 void Dialog::checkStatus()
217 // buffer independant dialogs are always active.
218 // This check allows us leave canApply unimplemented for some dialogs.
219 if (!isBufferDependent()) {
224 // deactivate the dialog if we have no buffer
225 if (!isBufferAvailable()) {
230 // check whether this dialog may be active
232 bool const readonly = isBufferReadonly();
233 enableView(!readonly);
234 // refreshReadOnly() is too generous in _enabling_ widgets
235 // update dialog to disable disabled widgets again
237 if (!readonly || canApplyToReadOnly())
245 QString Dialog::sessionKey() const
247 return "views/" + QString::number(lyxview_->id())
252 void Dialog::saveSession() const
255 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
259 void Dialog::restoreSession()
262 asQWidget()->restoreGeometry(
263 settings.value(sessionKey() + "/geometry").toByteArray());
266 } // namespace frontend