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 "BufferParams.h"
20 #include "BufferView.h"
22 #include "FuncRequest.h"
23 #include "FuncStatus.h"
26 #include "support/debug.h"
27 #include "support/gettext.h"
28 #include "support/lassert.h"
35 using namespace lyx::support;
40 Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
41 : name_(name), title_(title), lyxview_(lv)
45 bool Dialog::canApply() const
47 FuncRequest const fr(getLfun(), fromqstr(name_));
48 FuncStatus const fs(getStatus(fr));
53 void Dialog::dispatch(FuncRequest const & fr) const
59 void Dialog::updateDialog() const
61 dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
65 void Dialog::disconnect() const
67 lyxview_.disconnectDialog(fromqstr(name_));
71 bool Dialog::isBufferAvailable() const
73 return lyxview_.currentBufferView() != nullptr;
77 bool Dialog::isBufferReadonly() const
79 if (!lyxview_.documentBufferView())
81 return lyxview_.documentBufferView()->buffer().isReadonly();
85 QString Dialog::bufferFilePath() const
87 return toqstr(buffer().filePath());
91 KernelDocType Dialog::docType() const
93 if (buffer().params().isLatex())
94 return KernelDocType::LaTeX;
95 if (buffer().params().isLiterate())
96 return KernelDocType::Literate;
98 // This case should not happen.
99 return KernelDocType::LaTeX;
103 BufferView const * Dialog::bufferview() const
105 return lyxview_.currentBufferView();
109 Buffer const & Dialog::buffer() const
111 LAPPERR(lyxview_.currentBufferView());
112 return lyxview_.currentBufferView()->buffer();
116 Buffer const & Dialog::documentBuffer() const
118 LAPPERR(lyxview_.documentBufferView());
119 return lyxview_.documentBufferView()->buffer();
123 void Dialog::showData(string const & data, Qt::FocusReason reason)
125 if (isBufferDependent() && !isBufferAvailable())
128 if (!initialiseParams(data)) {
129 LYXERR0("Dialog \"" << name()
130 << "\" failed to translate the data string passed to show()");
140 if (isBufferDependent()) {
141 if (!isBufferAvailable() ||
142 (isBufferReadonly() && !canApplyToReadOnly()))
149 if (disconnectOnApply() && !isClosing()) {
151 initialiseParams(string());
157 void Dialog::prepareView()
159 // Make sure the dialog controls are correctly enabled/disabled with
163 QWidget * w = asQWidget();
164 w->setWindowTitle(title_);
166 QSize const hint = w->sizeHint();
167 if (hint.height() >= 0 && hint.width() >= 0)
168 w->setMinimumSize(hint);
172 void Dialog::showView(Qt::FocusReason reason)
176 QWidget * w = asQWidget();
177 if (!w->isVisible()) {
183 if (wantInitialFocus())
187 lyxview_.activateWindow();
188 lyxview_.setFocus(reason);
192 void Dialog::hideView()
194 QWidget * w = asQWidget();
203 bool Dialog::isVisibleView() const
205 return asQWidget()->isVisible();
209 Inset const * Dialog::inset(InsetCode code) const
211 // ins: the innermost inset of the type we look for
212 // that contains the cursor
213 Inset * ins = bufferview()->cursor().innerInsetOfType(code);
214 // next: a potential inset at cursor position
215 Inset * next = bufferview()->cursor().nextInset();
216 // Check if next is of the type we look for
218 if (next->lyxCode() != code)
221 // prefer next if it is of the requested type (bug 8716)
225 // no containing inset of requested type
226 // use next (which might also be 0)
232 void Dialog::checkStatus()
234 // buffer independent dialogs are always active.
235 // This check allows us leave canApply unimplemented for some dialogs.
236 if (!isBufferDependent()) {
241 // deactivate the dialog if we have no buffer
242 if (!isBufferAvailable()) {
247 // check whether this dialog may be active
249 bool const readonly = isBufferReadonly();
250 enableView(!readonly || canApplyToReadOnly());
257 QString Dialog::sessionKey() const
259 return "views/" + QString::number(lyxview_.id())
264 void Dialog::saveSession(QSettings & settings) const
266 settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
270 void Dialog::restoreSession()
273 asQWidget()->restoreGeometry(
274 settings.value(sessionKey() + "/geometry").toByteArray());
278 // If we have just created an inset, then we want to attach the
279 // dialog to it. This (i) allows further modification of that inset and
280 // (ii) prevents an additional click on Apply or OK from unexpectedly
281 // creating another inset. (See #3964 and #11030.)
282 void Dialog::connectToNewInset()
284 GuiView & view = const_cast<GuiView &>(lyxview());
285 BufferView * bv = view.currentBufferView();
286 // should have one, but just to be safe...
290 // are we attached to an inset already?
291 Inset * ins = bv->editedInset(fromqstr(name_));
295 // no, so we just inserted one, and now we are behind it.
296 Cursor const & cur = bv->cursor();
297 ins = cur.prevInset();
299 bv->editInset(fromqstr(name_), ins);
302 } // namespace frontend