mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
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>
This commit is contained in:
parent
2f759f52e1
commit
a412753b0a
3 changed files with 222 additions and 1 deletions
2
Makefile
2
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' $<
|
||||
|
|
120
qt-gui.cpp
120
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();
|
||||
|
|
101
ui/main-window.ui
Normal file
101
ui/main-window.ui
Normal file
|
@ -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>
|
Loading…
Add table
Reference in a new issue