Added settings to Qt4.
[mp-5.x.git] / mpv_qt4.cpp
blobdc802ed972595b92ee48caa115eaa025fbb1b8e7
1 /*
3 Minimum Profit - Programmer Text Editor
5 Qt4 driver.
7 Copyright (C) 2009 Angel Ortega <angel@triptico.com>
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 http://www.triptico.com
27 /* override auto-generated definition in config.h */
28 extern "C" int qt4_drv_detect(int * argc, char *** argv);
30 #include "config.h"
32 #include <stdio.h>
33 #include <wchar.h>
34 #include <unistd.h>
35 #include "mpdm.h"
36 #include "mpsl.h"
37 #include "mp.h"
38 #include "mp.xpm"
40 #include <QtGui>
42 /** data **/
44 class MPWindow : public QMainWindow
46 public:
47 MPWindow(QWidget *parent = 0);
48 bool queryExit(void);
49 bool event(QEvent *event);
51 QSettings *settings;
54 class MPArea : public QWidget
56 Q_OBJECT
58 public:
59 MPArea(QWidget *parent = 0);
60 void inputMethodEvent(QInputMethodEvent *event);
61 void keyPressEvent(QKeyEvent *event);
62 void keyReleaseEvent(QKeyEvent *event);
63 void mousePressEvent(QMouseEvent *event);
64 void mouseReleaseEvent(QMouseEvent *event);
65 void mouseMoveEvent(QMouseEvent *event);
66 void wheelEvent(QWheelEvent *event);
67 void dragEnterEvent(QDragEnterEvent *event);
68 void dropEvent(QDropEvent *event);
69 bool event(QEvent *event);
71 protected:
72 void paintEvent(QPaintEvent *event);
74 public slots:
75 void from_scrollbar(int);
76 void from_filetabs(int);
77 void from_menu(QAction *);
80 /* global data */
81 QApplication *app;
82 MPWindow *window;
83 QMenuBar *menubar;
84 QStatusBar *statusbar;
85 QTabBar *file_tabs;
87 #include "mpv_qk_common.cpp"
89 static void build_menu(void)
90 /* builds the menu */
95 static void draw_status(void)
97 statusbar->showMessage(str_to_qstring(mp_build_status_line()));
101 /** MPWindow methods **/
103 MPWindow::MPWindow(QWidget *parent) : QMainWindow(parent)
105 setWindowTitle("MP " VERSION);
107 menubar = this->menuBar();
108 build_menu();
110 statusbar = this->statusBar();
112 /* the full container */
113 QVBoxLayout *vb = new QVBoxLayout(this);
115 file_tabs = new QTabBar();
116 file_tabs->setFocusPolicy(Qt::NoFocus);
118 QHBoxLayout *hb = new QHBoxLayout();
120 /* main area */
121 area = new MPArea();
122 scrollbar = new QScrollBar();
123 scrollbar->setFocusPolicy(Qt::NoFocus);
125 hb->addWidget(area);
126 hb->addWidget(scrollbar);
127 QWidget *cc = new QWidget();
128 cc->setLayout(hb);
130 vb->addWidget(file_tabs);
131 vb->addWidget(cc);
132 QWidget *mc = new QWidget();
133 mc->setLayout(vb);
135 setCentralWidget(mc);
137 connect(scrollbar, SIGNAL(valueChanged(int)),
138 area, SLOT(from_scrollbar(int)));
140 connect(file_tabs, SIGNAL(currentChanged(int)),
141 area, SLOT(from_filetabs(int)));
143 connect(menubar, SIGNAL(triggered(QAction *)),
144 area, SLOT(from_menu(QAction *)));
146 this->setWindowIcon(QIcon(QPixmap(mp_xpm)));
148 settings = new QSettings("triptico.com", "MinimumProfit");
150 QPoint pos = settings->value("pos", QPoint(20, 20)).toPoint();
151 QSize size = settings->value("size", QSize(600, 400)).toSize();
153 move(pos);
154 resize(size);
158 static void save_settings(MPWindow *w)
160 w->settings->setValue("pos", w->pos());
161 w->settings->setValue("size", w->size());
162 w->settings->sync();
166 bool MPWindow::queryExit(void)
168 mp_process_event(MPDM_LS(L"close-window"));
170 save_settings(this);
172 return mp_exit_requested ? true : false;
176 bool MPWindow::event(QEvent *event)
178 /* do the processing */
179 bool r = QWidget::event(event);
181 if (mp_exit_requested) {
182 save_settings(this);
183 exit(0);
186 return r;
190 /** driver functions **/
192 static mpdm_t qt4_drv_alert(mpdm_t a)
194 return NULL;
198 static mpdm_t qt4_drv_confirm(mpdm_t a)
200 return NULL;
204 static mpdm_t qt4_drv_openfile(mpdm_t a)
206 QString r;
207 char tmp[128];
209 getcwd(tmp, sizeof(tmp));
211 /* 1# arg: prompt */
212 r = QFileDialog::getOpenFileName(window,
213 str_to_qstring(mpdm_aget(a, 0)), tmp);
215 return qstring_to_str(r);
219 static mpdm_t qt4_drv_savefile(mpdm_t a)
221 QString r;
222 char tmp[128];
224 getcwd(tmp, sizeof(tmp));
226 /* 1# arg: prompt */
227 r = QFileDialog::getSaveFileName(window,
228 str_to_qstring(mpdm_aget(a, 0)), tmp);
230 return qstring_to_str(r);
234 static mpdm_t qt4_drv_form(mpdm_t a)
236 return NULL;
240 static void register_functions(void)
242 mpdm_t drv;
244 drv = mpdm_hget_s(mp, L"drv");
245 mpdm_hset_s(drv, L"main_loop", MPDM_X(qt4_drv_main_loop));
246 mpdm_hset_s(drv, L"shutdown", MPDM_X(qt4_drv_shutdown));
248 mpdm_hset_s(drv, L"clip_to_sys", MPDM_X(qt4_drv_clip_to_sys));
249 mpdm_hset_s(drv, L"sys_to_clip", MPDM_X(qt4_drv_sys_to_clip));
250 mpdm_hset_s(drv, L"update_ui", MPDM_X(qt4_drv_update_ui));
251 /* mpdm_hset_s(drv, L"timer", MPDM_X(qt4_drv_timer));*/
252 mpdm_hset_s(drv, L"busy", MPDM_X(qt4_drv_busy));
254 mpdm_hset_s(drv, L"alert", MPDM_X(qt4_drv_alert));
255 mpdm_hset_s(drv, L"confirm", MPDM_X(qt4_drv_confirm));
256 mpdm_hset_s(drv, L"openfile", MPDM_X(qt4_drv_openfile));
257 mpdm_hset_s(drv, L"savefile", MPDM_X(qt4_drv_savefile));
258 mpdm_hset_s(drv, L"form", MPDM_X(qt4_drv_form));
262 static mpdm_t qt4_drv_startup(mpdm_t a)
263 /* driver initialization */
265 register_functions();
267 build_font(1);
268 build_colors();
270 window = new MPWindow();
271 window->show();
273 return NULL;
276 extern "C" Display *XOpenDisplay(char *);
278 extern "C" int qt4_drv_detect(int * argc, char *** argv)
280 mpdm_t drv;
281 Display *x11_display;
283 /* try connecting directly to the Xserver */
284 if ((x11_display = XOpenDisplay((char *)NULL)) == NULL)
285 return 0;
287 /* this is where it crashes if no X server */
288 app = new QApplication(x11_display);
290 drv = mpdm_hget_s(mp, L"drv");
291 mpdm_hset_s(drv, L"id", MPDM_LS(L"qt4"));
292 mpdm_hset_s(drv, L"startup", MPDM_X(qt4_drv_startup));
294 return 1;
297 #include "mpv_qt4.moc"