]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/DialogView.h
put widget in layout
[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 "controllers/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                 MyController * c = new MyController(*this);
48                 controller_ = c;
49                 controller_->setLyXView(parent);
50                 QGridLayout * gridLayout = new QGridLayout(this);
51                 gridLayout->setMargin(0);
52                 widget_ = new MyWidget(*c, this);
53                 gridLayout->addWidget(widget_);
54                 setWindowTitle("LyX: " + widget_->windowTitle());
55         }
56
57         /// Dialog inherited methods
58         //@{
59         void applyView() {}
60         void hideView()
61         {
62                 controller().clearParams();
63                 QDialog::hide();
64         }
65         void showData(std::string const & data)
66         {
67                 controller_->initialiseParams(data);
68                 showView();
69         }
70         void showView()
71         {
72                 widget_->updateView();  // make sure its up-to-date
73                 QDialog::show();
74                 raise();
75                 activateWindow();
76         }
77         bool isVisibleView() const { return QDialog::isVisible(); }
78         void checkStatus() { updateView(); }
79         void redraw() { redrawView(); }
80         void redrawView() {}
81         void updateData(std::string const & data)
82         {
83                 controller_->initialiseParams(data);
84                 updateView();
85         }
86         void updateView()
87         {
88                 widget_->updateView();
89         }
90         void partialUpdateView(int /*id*/) {}
91         Controller & controller() { return *controller_; }
92         std::string name() const { return name_; }
93         //@}
94 private:
95         /// The encapsulated widget.
96         MyWidget * widget_;
97         Controller * controller_;
98         std::string name_;
99
100         void showEvent(QShowEvent * e)
101         {
102 #if (QT_VERSION >= 0x040200)
103                 QSettings settings;
104                 std::string key = name_ + "/geometry";
105                 QDialog::restoreGeometry(settings.value(key.c_str()).toByteArray());
106 #endif
107             QDialog::showEvent(e);
108         }
109
110         void closeEvent(QCloseEvent * e)
111         {
112 #if (QT_VERSION >= 0x040200)
113                 QSettings settings;
114                 std::string key = name_ + "/geometry";
115                 settings.setValue(key.c_str(), QDialog::saveGeometry());
116 #endif
117             QDialog::closeEvent(e);
118         }
119 };
120
121 } // frontend
122 } // lyx
123
124 #endif // DIALOG_VIEW_H