]> git.lyx.org Git - features.git/blob - src/frontends/qt2/Qt2Base.h
a6a660b4257e72ef6981eee1d05f34131d25287c
[features.git] / src / frontends / qt2 / Qt2Base.h
1 // -*- C++ -*-
2 /* This file is part of
3  * ======================================================
4  *
5  *                 LyX, The Document Processor
6  *
7  *                 Copyright 2000 The LyX Team.
8  *
9  * ======================================================
10  *
11  * \author Angus Leeming <a.leeming@ic.ac.uk>
12  */
13
14 #ifndef QT2BASE_H
15 #define QT2BASE_H
16
17 #include <config.h> 
18  
19 #include <qfont.h>
20 #include <qdialog.h>
21 #include <qobject.h>
22 #include <qapplication.h>
23
24 #ifdef __GNUG__
25 #pragma interface
26 #endif
27
28 #include "ViewBase.h"
29 #include "LString.h"
30 #include "ButtonPolicies.h"
31 #include "ControlButtons.h"
32
33 #include <boost/smart_ptr.hpp>
34
35 class Qt2BC;
36
37 /** This class is an Qt2 GUI base class.
38  */
39 class Qt2Base : public QObject, public ViewBC<Qt2BC>
40 {
41         Q_OBJECT
42 public:
43         ///
44         Qt2Base(ControlButtons &, const QString &);
45         ///
46         virtual ~Qt2Base() {}
47
48 protected:
49         /// Build the dialog
50         virtual void build() = 0;
51         /// Hide the dialog.
52         virtual void hide();
53         /// Create the dialog if necessary, update it and display it.
54         virtual void show();
55         /// update the dialog's contents
56         virtual void update_contents() = 0; 
57
58         /// the dialog has changed contents
59         virtual void changed(); 
60
61         /// is the dialog currently valid ? 
62         virtual bool isValid();
63
64         /// are we updating ?
65         bool updating_; 
66  
67 protected slots:
68         // dialog closed from WM
69         void slotWMHide();
70
71         // Restore button clicked
72         void slotRestore();
73  
74         // OK button clicked
75         void slotOK();
76
77         // Apply button clicked
78         void slotApply();
79
80         // Close button clicked
81         void slotClose();
82
83 private:
84         /// Pointer to the actual instantiation of xform's form
85         virtual QDialog* form() const = 0;
86
87 private:
88         /// dialog title, displayed by WM.
89         QString title_;
90 };
91
92
93 template <class Dialog>
94 class Qt2DB: public Qt2Base
95 {
96 protected:
97         Qt2DB(ControlButtons &, const QString &);
98  
99         /// update the dialog 
100         virtual void update();
101  
102         /// Pointer to the actual instantiation of the Qt dialog
103         virtual QDialog * form() const;
104         /// Real GUI implementation.
105         boost::scoped_ptr<Dialog> dialog_;
106
107 };
108
109
110 template <class Dialog>
111 Qt2DB<Dialog>::Qt2DB(ControlButtons & c, const QString & t)
112         : Qt2Base(c, t)
113 {}
114
115
116 template <class Dialog>
117 QDialog * Qt2DB<Dialog>::form() const
118 {
119         return dialog_.get();
120 }
121
122
123 template <class Dialog>
124 void Qt2DB<Dialog>::update()
125 {
126         form()->setUpdatesEnabled(false);
127  
128         // this is tricky. First we process pending events
129         // as a result of the bc() state change (if any).
130         // then we lock out any bc() changes as a result of
131         // "innocent" updates during update_contents()
132         // then we enable normal behaviour again.
133         qApp->processEvents();
134         updating_ = true;
135         update_contents();
136         qApp->processEvents();
137         updating_ = false;
138  
139         form()->setUpdatesEnabled(true);
140         form()->update();
141 }
142
143  
144 template <class Controller, class Base>
145 class Qt2CB: public Base
146 {
147 protected:
148         ///
149         Qt2CB(ControlButtons &, const QString&);
150         /// The parent controller
151         Controller & controller() const;
152 };
153
154
155 template <class Controller, class Base>
156 Qt2CB<Controller, Base>::Qt2CB(ControlButtons & c, const QString& t)
157         : Base(c, t)
158 {}
159
160
161 template <class Controller, class Base>
162 Controller & Qt2CB<Controller, Base>::controller() const
163 {
164         return static_cast<Controller &>(controller_);
165         //return dynamic_cast<Controller &>(controller_);
166 }
167
168
169 #endif // FORMBASE_H