summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Alberto Mardegan <mardy@users.sourceforge.net>2013-04-02 19:49:17 +0300
committerGravatar Alberto Mardegan <mardy@users.sourceforge.net>2013-04-02 19:55:53 +0300
commita412753b0a2eb6323f350e98287b004f5b3b6c5c (patch)
tree70248799a99333a88df813dd79fa061a5e5fb59e
parent2f759f52e12e2e065f6022797dc51106a7093899 (diff)
downloadsubsurface-a412753b0a2eb6323f350e98287b004f5b3b6c5c.tar.gz
Add a Qt main window
This is just an empty window with a File menu and a few items. It shows how to hook up functions to menu actions. Signed-off-by: Alberto Mardegan <mardy@users.sourceforge.net>
-rw-r--r--Makefile2
-rw-r--r--qt-gui.cpp120
-rw-r--r--ui/main-window.ui101
3 files changed, 222 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index c0114386c..59e2f9522 100644
--- a/Makefile
+++ b/Makefile
@@ -322,7 +322,7 @@ MOCFLAGS = $(filter -I%, $(CXXFLAGS) $(EXTRA_FLAGS)) $(filter -D%, $(CXXFLAGS) $
@echo ' MOC' $<
@$(MOC) -i $(MOCFLAGS) $< -o $@
-qt-gui.o: qt-gui.moc
+qt-gui.o: main-window.ui.h qt-gui.moc
%.ui.h: ui/%.ui
@echo ' UIC' $<
diff --git a/qt-gui.cpp b/qt-gui.cpp
index 2e3f2c903..285f2082d 100644
--- a/qt-gui.cpp
+++ b/qt-gui.cpp
@@ -24,10 +24,14 @@
#include "webservice.h"
#include "version.h"
#include "libdivecomputer.h"
+#include "main-window.ui.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gdk-pixdata.h>
#include <QApplication>
+#include <QFileDialog>
+#include <QFileInfo>
+#include <QStringList>
#include <QTranslator>
#if HAVE_OSM_GPS_MAP
@@ -1733,6 +1737,120 @@ static gboolean notebook_tooltip (GtkWidget *widget, gint x, gint y,
}
}
+class MainWindow: public QMainWindow, private Ui::MainWindow
+{
+ Q_OBJECT
+
+public:
+ MainWindow(QWidget *parent = 0);
+ ~MainWindow() {}
+
+ void setCurrentFileName(const QString &fileName);
+
+private Q_SLOTS:
+ void on_actionNew_triggered() { on_actionClose_triggered(); }
+ void on_actionOpen_triggered();
+ void on_actionSave_triggered() { file_save(NULL, NULL); }
+ void on_actionSaveAs_triggered() { file_save_as(NULL, NULL); }
+ void on_actionClose_triggered();
+
+private:
+ QStringList fileNameFilters() const;
+
+private:
+ QString m_currentFileName;
+};
+
+MainWindow::MainWindow(QWidget *parent):
+ QMainWindow(parent)
+{
+ setupUi(this);
+}
+
+void MainWindow::setCurrentFileName(const QString &fileName)
+{
+ if (fileName == m_currentFileName) return;
+ m_currentFileName = fileName;
+
+ QString title = tr("Subsurface");
+ if (!m_currentFileName.isEmpty()) {
+ QFileInfo fileInfo(m_currentFileName);
+ title += " - " + fileInfo.fileName();
+ }
+ setWindowTitle(title);
+}
+
+void MainWindow::on_actionOpen_triggered()
+{
+ QString defaultFileName = QString::fromUtf8(prefs.default_filename);
+ QFileInfo fileInfo(defaultFileName);
+
+ QFileDialog dialog(this, tr("Open File"), fileInfo.path());
+ dialog.setFileMode(QFileDialog::ExistingFile);
+ dialog.selectFile(defaultFileName);
+ dialog.setNameFilters(fileNameFilters());
+ if (dialog.exec()) {
+ /* first, close the existing file, if any */
+ file_close(NULL, NULL);
+
+ /* we know there is only one filename */
+ QString fileName = dialog.selectedFiles().first();
+ GError *error = NULL;
+ parse_file(fileName.toUtf8().constData(), &error);
+ if (error != NULL) {
+ report_error(error);
+ g_error_free(error);
+ error = NULL;
+ } else {
+ setCurrentFileName(fileName);
+ }
+ report_dives(FALSE, FALSE);
+ }
+}
+
+void MainWindow::on_actionClose_triggered()
+{
+ if (unsaved_changes())
+ if (ask_save_changes() == FALSE)
+ return;
+
+ setCurrentFileName(QString());
+
+ /* free the dives and trips */
+ while (dive_table.nr)
+ delete_single_dive(0);
+ mark_divelist_changed(FALSE);
+
+ /* clear the selection and the statistics */
+ selected_dive = 0;
+ process_selected_dives();
+ clear_stats_widgets();
+ clear_events();
+ show_dive_stats(NULL);
+
+ /* clear the equipment page */
+ clear_equipment_widgets();
+
+ /* redraw the screen */
+ dive_list_update_dives();
+ show_dive_info(NULL);
+}
+
+QStringList MainWindow::fileNameFilters() const
+{
+ QStringList filters;
+
+ filters << "*.xml *.uddf *.udcf *.jlb"
+#ifdef LIBZIP
+ " *.sde *.dld"
+#endif
+#ifdef SQLITE3
+ " *.db"
+#endif
+ ;
+ return filters;
+}
+
void init_ui(int *argcp, char ***argvp)
{
GtkWidget *win;
@@ -1748,6 +1866,8 @@ void init_ui(int *argcp, char ***argvp)
application = new QApplication(*argcp, *argvp);
application->installTranslator(new Translator(application));
+ MainWindow *window = new MainWindow();
+ window->show();
gtk_init(argcp, argvp);
settings = gtk_settings_get_default();
diff --git a/ui/main-window.ui b/ui/main-window.ui
new file mode 100644
index 000000000..58ed198fd
--- /dev/null
+++ b/ui/main-window.ui
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>600</height>
+ </rect>
+ </property>
+ <property name="windowTitle">
+ <string>Subsurface</string>
+ </property>
+ <widget class="QWidget" name="centralwidget"/>
+ <widget class="QMenuBar" name="menubar">
+ <property name="geometry">
+ <rect>
+ <x>0</x>
+ <y>0</y>
+ <width>800</width>
+ <height>23</height>
+ </rect>
+ </property>
+ <widget class="QMenu" name="menuFile">
+ <property name="title">
+ <string>File</string>
+ </property>
+ <addaction name="actionNew"/>
+ <addaction name="actionOpen"/>
+ <addaction name="actionSave"/>
+ <addaction name="actionSaveAs"/>
+ <addaction name="actionClose"/>
+ </widget>
+ <addaction name="menuFile"/>
+ </widget>
+ <widget class="QStatusBar" name="statusbar"/>
+ <action name="actionNew">
+ <property name="icon">
+ <iconset theme="document-new">
+ <normaloff/>
+ </iconset>
+ </property>
+ <property name="text">
+ <string>New</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+N</string>
+ </property>
+ </action>
+ <action name="actionOpen">
+ <property name="icon">
+ <iconset theme="document-open">
+ <normaloff/>
+ </iconset>
+ </property>
+ <property name="text">
+ <string>Open...</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+O</string>
+ </property>
+ </action>
+ <action name="actionSave">
+ <property name="icon">
+ <iconset theme="document-save"/>
+ </property>
+ <property name="text">
+ <string>Save...</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+S</string>
+ </property>
+ </action>
+ <action name="actionSaveAs">
+ <property name="icon">
+ <iconset theme="document-save-as"/>
+ </property>
+ <property name="text">
+ <string>Save As...</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+Shift+S</string>
+ </property>
+ </action>
+ <action name="actionClose">
+ <property name="icon">
+ <iconset theme="window-close"/>
+ </property>
+ <property name="text">
+ <string>Close</string>
+ </property>
+ <property name="shortcut">
+ <string>Ctrl+W</string>
+ </property>
+ </action>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>