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
162 QWidget * w = asQWidget();
163 w->setWindowTitle(title_);
165 QSize const hint = w->sizeHint();
166 if (hint.height() >= 0 && hint.width() >= 0)
167 w->setMinimumSize(hint);
169 if (w->isVisible()) {
175 if (wantInitialFocus())
179 lyxview_->activateWindow();
180 lyxview_->setFocus();
185 void Dialog::hideView()
187 QWidget * w = asQWidget();
196 bool Dialog::isVisibleView() const
198 return asQWidget()->isVisible();
202 Inset const * Dialog::inset(InsetCode code) const
204 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
206 ins = bufferview()->cursor().nextInset();
207 if (!ins || ins->lyxCode() != code)
213 void Dialog::checkStatus()
215 // buffer independant dialogs are always active.
216 // This check allows us leave canApply unimplemented for some dialogs.
217 if (!isBufferDependent()) {
222 // deactivate the dialog if we have no buffer
223 if (!isBufferAvailable()) {
228 // check whether this dialog may be active
230 bool const readonly = isBufferReadonly();
231 enableView(!readonly || canApplyToReadOnly());
232 // refreshReadOnly() is too generous in _enabling_ widgets
233 // update dialog to disable disabled widgets again
235 if (!readonly || canApplyToReadOnly())
243 QString Dialog::sessionKey() const
245 return "views/" + QString::number(lyxview_->id())
250 void Dialog::saveSession() const
253 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
257 void Dialog::restoreSession()
260 asQWidget()->restoreGeometry(
261 settings.value(sessionKey() + "/geometry").toByteArray());
264 } // namespace frontend