]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/Dialog.cpp
* fix spelling in comments to please John.
[lyx.git] / src / frontends / qt4 / Dialog.cpp
index c26b38f468e0498d84aae376ec5b0b3a14e7ccfe..65d4a5598141dbb3471e02af0c1471a2e638051c 100644 (file)
 #include "Dialog.h"
 
 #include "GuiView.h"
+#include "qt_helpers.h"
 
 #include "Buffer.h"
+#include "BufferView.h"
+#include "Cursor.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
 
-#include "support/Debug.h"
+#include "insets/Inset.h"
+
+#include "support/debug.h"
+#include "support/lassert.h"
+
+#include <QSettings>
+#include <QString>
 
 #include <string>
 
 using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
 
-Dialog::Dialog(GuiView & lv, std::string const & name)
-       : name_(name), lyxview_(&lv)
+Dialog::Dialog(GuiView & lv, QString const & name, QString const & title)
+       : name_(name), title_(title), lyxview_(&lv)
 {}
 
 
@@ -38,14 +48,9 @@ Dialog::~Dialog()
 {}
 
 
-std::string const & Dialog::name() const
-{
-       return name_;
-}
-
 bool Dialog::canApply() const
 {
-       FuncRequest const fr(getLfun(), from_ascii(name_));
+       FuncRequest const fr(getLfun(), fromqstr(name_));
        FuncStatus const fs(getStatus(fr));
        return fs.enabled();
 }
@@ -53,40 +58,39 @@ bool Dialog::canApply() const
 
 void Dialog::dispatch(FuncRequest const & fr) const
 {
-       theLyXFunc().setLyXView(lyxview_);
        lyx::dispatch(fr);
 }
 
 
 void Dialog::updateDialog() const
 {
-       dispatch(FuncRequest(LFUN_DIALOG_UPDATE, from_ascii(name_)));
+       dispatch(FuncRequest(LFUN_DIALOG_UPDATE, fromqstr(name_)));
 }
 
 
 void Dialog::disconnect() const
 {
-       lyxview_->disconnectDialog(name_);
+       lyxview_->disconnectDialog(fromqstr(name_));
 }
 
 
 bool Dialog::isBufferAvailable() const
 {
-       return lyxview_->buffer() != 0;
+       return lyxview_->currentBufferView() != 0;
 }
 
 
 bool Dialog::isBufferReadonly() const
 {
-       if (!lyxview_->buffer())
+       if (!lyxview_->documentBufferView())
                return true;
-       return lyxview_->buffer()->isReadonly();
+       return lyxview_->documentBufferView()->buffer().isReadonly();
 }
 
 
-std::string const Dialog::bufferFilepath() const
+QString Dialog::bufferFilepath() const
 {
-       return buffer().filePath();
+       return toqstr(buffer().filePath());
 }
 
 
@@ -101,29 +105,16 @@ KernelDocType Dialog::docType() const
 }
 
 
-BufferView * Dialog::bufferview()
-{
-       return lyxview_->view();
-}
-
-
 BufferView const * Dialog::bufferview() const
 {
-       return lyxview_->view();
-}
-
-
-Buffer & Dialog::buffer()
-{
-       BOOST_ASSERT(lyxview_->buffer());
-       return *lyxview_->buffer();
+       return lyxview_->currentBufferView();
 }
 
 
 Buffer const & Dialog::buffer() const
 {
-       BOOST_ASSERT(lyxview_->buffer());
-       return *lyxview_->buffer();
+       LASSERT(lyxview_->currentBufferView(), /**/);
+       return lyxview_->currentBufferView()->buffer();
 }
 
 
@@ -161,39 +152,39 @@ void Dialog::apply()
 }
 
 
-void Dialog::updateData(string const & data)
+void Dialog::prepareView()
 {
-       if (isBufferDependent() && !isBufferAvailable())
-               return;
+       // Make sure the dialog controls are correctly enabled/disabled with
+       // readonly status.
+       checkStatus();
 
-       if (!initialiseParams(data)) {
-               LYXERR0("Dialog \"" << name()
-                      << "\" could not be initialized");
-               return;
-       }
+       QWidget * w = asQWidget();
+       w->setWindowTitle(title_);
 
-       updateView();
+       QSize const hint = w->sizeHint();
+       if (hint.height() >= 0 && hint.width() >= 0)
+               w->setMinimumSize(hint);
 }
 
 
 void Dialog::showView()
 {
-       updateView();  // make sure its up-to-date
-       if (exitEarly())
-               return;
+       prepareView();
 
        QWidget * w = asQWidget();
-       QSize const hint = w->sizeHint();
-       if (hint.height() >= 0 && hint.width() >= 0)
-               w->setMinimumSize(hint);
-
        if (w->isVisible()) {
                w->raise();
                w->activateWindow();
        } else
                w->show();
 
-       w->setFocus();
+       if (wantInitialFocus())
+               w->setFocus();
+       else {
+               lyxview_->raise();
+               lyxview_->activateWindow();
+               lyxview_->setFocus();
+       }
 }
 
 
@@ -214,12 +205,25 @@ bool Dialog::isVisibleView() const
 }
 
 
+Inset const * Dialog::inset(InsetCode code) const
+{
+       Inset * ins = bufferview()->cursor().innerInsetOfType(code);
+       if (!ins)
+               ins = bufferview()->cursor().nextInset();
+       if (!ins || ins->lyxCode() != code)
+               return 0;
+       return ins;
+}
+
+
 void Dialog::checkStatus()
 {
-       // buffer independant dialogs are always active.
+       // buffer independent dialogs are always active.
        // This check allows us leave canApply unimplemented for some dialogs.
-       if (!isBufferDependent())
+       if (!isBufferDependent()) {
+               updateView();
                return;
+       }
 
        // deactivate the dialog if we have no buffer
        if (!isBufferAvailable()) {
@@ -230,16 +234,36 @@ void Dialog::checkStatus()
        // check whether this dialog may be active
        if (canApply()) {
                bool const readonly = isBufferReadonly();
-               enableView(!readonly);
+               enableView(!readonly || canApplyToReadOnly());
                // refreshReadOnly() is too generous in _enabling_ widgets
                // update dialog to disable disabled widgets again
 
-               if (!readonly || canApplyToReadOnly())
-                       updateView();
-
+               updateView();
        } else
                enableView(false);
 }
 
+
+QString Dialog::sessionKey() const
+{
+       return "views/" + QString::number(lyxview_->id())
+               + "/" + name();
+}
+
+
+void Dialog::saveSession() const
+{
+       QSettings settings;
+       settings.setValue(sessionKey() + "/geometry", asQWidget()->saveGeometry());
+}
+
+
+void Dialog::restoreSession()
+{
+       QSettings settings;
+       asQWidget()->restoreGeometry(
+               settings.value(sessionKey() + "/geometry").toByteArray());
+}
+
 } // namespace frontend
 } // namespace lyx