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 "FuncRequest.h"
20 #include "FuncStatus.h"
23 #include "support/debug.h"
28 #include <boost/assert.hpp>
33 using namespace lyx::support;
39 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
40 : name_(name), title_(title), lyxview_(&lv)
48 bool Dialog::canApply() const
50 FuncRequest const fr(getLfun(), fromqstr(name_));
51 FuncStatus const fs(getStatus(fr));
56 void Dialog::dispatch(FuncRequest const & fr) const
58 theLyXFunc().setLyXView(lyxview_);
63 void Dialog::updateDialog() const
65 dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
69 void Dialog::disconnect() const
71 lyxview_->disconnectDialog(fromqstr(name_));
75 bool Dialog::isBufferAvailable() const
77 return lyxview_->buffer() != 0;
81 bool Dialog::isBufferReadonly() const
83 if (!lyxview_->buffer())
85 return lyxview_->buffer()->isReadonly();
89 QString Dialog::bufferFilepath() const
91 return toqstr(buffer().filePath());
95 KernelDocType Dialog::docType() const
97 if (buffer().isLatex())
99 if (buffer().isLiterate())
106 BufferView * Dialog::bufferview()
108 return lyxview_->view();
112 BufferView const * Dialog::bufferview() const
114 return lyxview_->view();
118 Buffer & Dialog::buffer()
120 BOOST_ASSERT(lyxview_->buffer());
121 return *lyxview_->buffer();
125 Buffer const & Dialog::buffer() const
127 BOOST_ASSERT(lyxview_->buffer());
128 return *lyxview_->buffer();
132 void Dialog::showData(string const & data)
134 if (isBufferDependent() && !isBufferAvailable())
137 if (!initialiseParams(data)) {
138 LYXERR0("Dialog \"" << fromqstr(name())
139 << "\" failed to translate the data string passed to show()");
149 if (isBufferDependent()) {
150 if (!isBufferAvailable() ||
151 (isBufferReadonly() && !canApplyToReadOnly()))
158 if (disconnectOnApply() && !isClosing()) {
160 initialiseParams(string());
166 void Dialog::updateData(string const & data)
168 if (isBufferDependent() && !isBufferAvailable())
171 if (!initialiseParams(data)) {
172 LYXERR0("Dialog \"" << fromqstr(name())
173 << "\" could not be initialized");
181 void Dialog::showView()
183 // Make sure the dialog is up-to-date.
185 // Make sure the dialog controls are correctly enabled/disabled with
191 QWidget * w = asQWidget();
192 w->setWindowTitle(title_);
194 QSize const hint = w->sizeHint();
195 if (hint.height() >= 0 && hint.width() >= 0)
196 w->setMinimumSize(hint);
198 if (w->isVisible()) {
208 void Dialog::hideView()
210 QWidget * w = asQWidget();
219 bool Dialog::isVisibleView() const
221 return asQWidget()->isVisible();
225 void Dialog::checkStatus()
227 // buffer independant dialogs are always active.
228 // This check allows us leave canApply unimplemented for some dialogs.
229 if (!isBufferDependent())
232 // deactivate the dialog if we have no buffer
233 if (!isBufferAvailable()) {
238 // check whether this dialog may be active
240 bool const readonly = isBufferReadonly();
241 enableView(!readonly);
242 // refreshReadOnly() is too generous in _enabling_ widgets
243 // update dialog to disable disabled widgets again
245 if (!readonly || canApplyToReadOnly())
253 QString Dialog::sessionKey() const
255 return "view-" + QString::number(lyxview_->id())
260 void Dialog::saveSession() const
263 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
267 void Dialog::restoreSession()
270 asQWidget()->restoreGeometry(
271 settings.value(sessionKey() + "/geometry").toByteArray());
274 } // namespace frontend