]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/DialogView.h
move Controller inheritance further up the tree
[lyx.git] / src / frontends / qt4 / DialogView.h
1 // -*- C++ -*-
2 /**
3  * \file DialogView.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef DIALOG_VIEW_H
13 #define DIALOG_VIEW_H
14
15 #include "Dialog.h"
16 #include "GuiView.h"
17 #include "qt_helpers.h"
18 #include "debug.h"
19
20 #include <QCloseEvent>
21 #include <QDialog>
22 #include <QSettings>
23 #include <QShowEvent>
24 #include <QGridLayout>
25
26 #include <string>
27
28 namespace lyx {
29 namespace frontend {
30
31 /// Window Dialog container for LyX dialogs.
32 /// This template class that encapsulates a given Widget inside a
33 /// QDialog and presents a Dialog interface
34 template<class MyController, class MyWidget>
35 class DialogView : public QDialog, public Dialog
36 {
37 public:
38         DialogView(
39                 GuiViewBase & parent, ///< the main window where to dock.
40                 std::string const & name, ///< dialog identifier.
41                 bool modal = false, ///< Window modality.
42                 Qt::WindowFlags flags = 0
43                 )
44                 : QDialog(&parent, flags), name_(name)
45         {
46                 setModal(modal);
47                 controller_ = new MyController(*this, parent);
48                 QGridLayout * gridLayout = new QGridLayout(this);
49                 gridLayout->setMargin(0);
50                 widget_ = new MyWidget(*controller_, this);
51                 gridLayout->addWidget(widget_);
52                 setWindowTitle("LyX: " + widget_->windowTitle());
53         }
54
55         /// Dialog inherited methods
56         //@{
57         void applyView() {}
58         void hideView()
59         {
60                 controller().clearParams();
61                 QDialog::hide();
62         }
63         void showData(std::string const & data)
64         {
65                 controller_->initialiseParams(data);
66                 showView();
67         }
68         void showView()
69         {
70                 widget_->updateView();  // make sure its up-to-date
71                 QDialog::show();
72                 raise();
73                 activateWindow();
74         }
75         bool isVisibleView() const { return QDialog::isVisible(); }
76         void checkStatus() { updateView(); }
77         void redraw() { redrawView(); }
78         void redrawView() {}
79         void updateData(std::string const & data)
80         {
81                 controller_->initialiseParams(data);
82                 updateView();
83         }
84         void updateView()
85         {
86                 widget_->updateView();
87         }
88         void partialUpdateView(int /*id*/) {}
89         Controller & controller() { return *controller_; }
90         std::string name() const { return name_; }
91         //@}
92 private:
93         /// The encapsulated widget.
94         MyWidget * widget_;
95         Controller * controller_;
96         std::string name_;
97
98         void showEvent(QShowEvent * e)
99         {
100 #if (QT_VERSION >= 0x040200)
101                 QSettings settings;
102                 std::string key = name_ + "/geometry";
103                 QDialog::restoreGeometry(settings.value(key.c_str()).toByteArray());
104 #endif
105             QDialog::showEvent(e);
106         }
107
108         void closeEvent(QCloseEvent * e)
109         {
110 #if (QT_VERSION >= 0x040200)
111                 QSettings settings;
112                 std::string key = name_ + "/geometry";
113                 settings.setValue(key.c_str(), QDialog::saveGeometry());
114 #endif
115             QDialog::closeEvent(e);
116         }
117 };
118
119 } // frontend
120 } // lyx
121
122 #endif // DIALOG_VIEW_H