diff options
Diffstat (limited to 'gtk-gui.c')
-rw-r--r-- | gtk-gui.c | 261 |
1 files changed, 223 insertions, 38 deletions
@@ -3,6 +3,11 @@ /* creates the window and overall layout * divelist, dive info, equipment and printing are handled in their own source files */ +/* + * This is the former qt-gui.cpp - so it already contains some Qt related + * functions. It's renamed back to gtk-ui.c to keep the old Gtk code + * around for reference in case we still need it... all that has now been + * ripped out of qt-gui.cpp */ #include <libintl.h> #include <glib/gi18n.h> #include <stdio.h> @@ -24,19 +29,52 @@ #include "webservice.h" #include "version.h" #include "libdivecomputer.h" +#include "qt-ui/mainwindow.h" #include <gdk-pixbuf/gdk-pixbuf.h> #include <gdk-pixbuf/gdk-pixdata.h> -#include "subsurface-icon.h" + +#include <QApplication> +#include <QFileDialog> +#include <QFileInfo> +#include <QStringList> +#include <QTextCodec> +#include <QTranslator> #include <osm-gps-map-source.h> +class Translator: public QTranslator +{ + Q_OBJECT + +public: + Translator(QObject *parent = 0); + ~Translator() {} + + virtual QString translate(const char *context, const char *sourceText, + const char *disambiguation = NULL) const; +}; + +Translator::Translator(QObject *parent): + QTranslator(parent) +{ +} + +QString Translator::translate(const char *context, const char *sourceText, + const char *disambiguation) const +{ + return gettext(sourceText); +} + +static const GdkPixdata subsurface_icon_pixbuf = {}; + GtkWidget *main_window; GtkWidget *main_vbox; GtkWidget *error_info_bar; GtkWidget *error_label; GtkWidget *vpane, *hpane; GtkWidget *notebook; +static QApplication *application = NULL; int error_count; const char *existing_filename; @@ -93,6 +131,7 @@ static void on_info_bar_response(GtkWidget *widget, gint response, void report_error(GError* error) { + qDebug("Warning: Calling GTK-Specific Code."); if (error == NULL) { return; @@ -216,10 +255,12 @@ static void file_save(GtkWidget *w, gpointer data) static gboolean ask_save_changes() { + //WARNING: Porting to Qt + qDebug("This method is being ported to Qt, please, stop using it. "); GtkWidget *dialog, *label, *content; gboolean quit = TRUE; dialog = gtk_dialog_new_with_buttons(_("Save Changes?"), - GTK_WINDOW(main_window), GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + GTK_WINDOW(main_window), GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, GTK_STOCK_NO, GTK_RESPONSE_NO, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, @@ -254,6 +295,7 @@ static gboolean ask_save_changes() static void file_close(GtkWidget *w, gpointer data) { + qDebug("Calling an already ported-to-qt Gtk method"); if (unsaved_changes()) if (ask_save_changes() == FALSE) return; @@ -282,8 +324,12 @@ static void file_close(GtkWidget *w, gpointer data) show_dive_info(NULL); } +//##################################################################### +//###### ALREAADY PORTED TO Qt. DELETE ME WHEN NOT MORE USERFUL. # +//##################################################################### static void file_open(GtkWidget *w, gpointer data) { + qDebug("Calling an already ported-to-qt Gtk method."); GtkWidget *dialog; GtkFileFilter *filter; const char *current_default; @@ -315,7 +361,7 @@ static void file_open(GtkWidget *w, gpointer data) fn_glist = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(dialog)); GError *error = NULL; - filename = fn_glist->data; + filename = (char *)fn_glist->data; parse_file(filename, &error); set_filename(filename, TRUE); if (error != NULL) @@ -605,16 +651,16 @@ void update_screen() update_dive_list_col_visibility(); } -UNITCALLBACK(set_meter, length, METERS) -UNITCALLBACK(set_feet, length, FEET) -UNITCALLBACK(set_bar, pressure, BAR) -UNITCALLBACK(set_psi, pressure, PSI) -UNITCALLBACK(set_liter, volume, LITER) -UNITCALLBACK(set_cuft, volume, CUFT) -UNITCALLBACK(set_celsius, temperature, CELSIUS) -UNITCALLBACK(set_fahrenheit, temperature, FAHRENHEIT) -UNITCALLBACK(set_kg, weight, KG) -UNITCALLBACK(set_lbs, weight, LBS) +UNITCALLBACK(set_meter, length, units::METERS) +UNITCALLBACK(set_feet, length, units::FEET) +UNITCALLBACK(set_bar, pressure, units::BAR) +UNITCALLBACK(set_psi, pressure, units::PSI) +UNITCALLBACK(set_liter, volume, units::LITER) +UNITCALLBACK(set_cuft, volume, units::CUFT) +UNITCALLBACK(set_celsius, temperature, units::CELSIUS) +UNITCALLBACK(set_fahrenheit, temperature, units::FAHRENHEIT) +UNITCALLBACK(set_kg, weight, units::KG) +UNITCALLBACK(set_lbs, weight, units::LBS) OPTIONCALLBACK(otu_toggle, prefs.visible_cols.otu) OPTIONCALLBACK(maxcns_toggle, prefs.visible_cols.maxcns) @@ -664,7 +710,7 @@ static gboolean gfhigh_edit(GtkWidget *w, GdkEvent *event, gpointer _data) static void event_toggle(GtkWidget *w, gpointer _data) { - gboolean *plot_ev = _data; + gboolean *plot_ev = (gboolean *)_data; *plot_ev = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)); } @@ -707,7 +753,7 @@ static void pick_default_file(GtkWidget *w, GtkButton *button) list = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(fs_dialog)); if (g_slist_length(list) == 1) - gtk_button_set_label(button, list->data); + gtk_button_set_label(button, (const gchar *)list->data); g_slist_free(list); } @@ -720,7 +766,7 @@ static void pick_default_file(GtkWidget *w, GtkButton *button) static GtkWidget * map_provider_widget() { - OsmGpsMapSource_t i; + int i; #if GTK_CHECK_VERSION(2,24,0) GtkWidget *combobox = gtk_combo_box_text_new(); @@ -772,28 +818,28 @@ static void preferences_dialog(GtkWidget *w, gpointer data) gtk_container_add(GTK_CONTAINER(frame), box); create_radio(box, _("Depth:"), - _("Meter"), set_meter, (prefs.units.length == METERS), - _("Feet"), set_feet, (prefs.units.length == FEET), + _("Meter"), set_meter, (prefs.units.length == units::METERS), + _("Feet"), set_feet, (prefs.units.length == units::FEET), NULL); create_radio(box, _("Pressure:"), - _("Bar"), set_bar, (prefs.units.pressure == BAR), - _("PSI"), set_psi, (prefs.units.pressure == PSI), + _("Bar"), set_bar, (prefs.units.pressure == units::BAR), + _("PSI"), set_psi, (prefs.units.pressure == units::PSI), NULL); create_radio(box, _("Volume:"), - _("Liter"), set_liter, (prefs.units.volume == LITER), - _("CuFt"), set_cuft, (prefs.units.volume == CUFT), + _("Liter"), set_liter, (prefs.units.volume == units::LITER), + _("CuFt"), set_cuft, (prefs.units.volume == units::CUFT), NULL); create_radio(box, _("Temperature:"), - _("Celsius"), set_celsius, (prefs.units.temperature == CELSIUS), - _("Fahrenheit"), set_fahrenheit, (prefs.units.temperature == FAHRENHEIT), + _("Celsius"), set_celsius, (prefs.units.temperature == units::CELSIUS), + _("Fahrenheit"), set_fahrenheit, (prefs.units.temperature == units::FAHRENHEIT), NULL); create_radio(box, _("Weight:"), - _("kg"), set_kg, (prefs.units.weight == KG), - _("lbs"), set_lbs, (prefs.units.weight == LBS), + _("kg"), set_kg, (prefs.units.weight == units::KG), + _("lbs"), set_lbs, (prefs.units.weight == units::LBS), NULL); frame = gtk_frame_new(_("Show Columns")); @@ -1072,14 +1118,14 @@ static void preferences_dialog(GtkWidget *w, gpointer data) prefs.default_filename = new_default; } /* get the map provider selected */ - OsmGpsMapSource_t i; + int i; #if GTK_CHECK_VERSION(2,24,0) char *provider = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(map_provider)); #else char *provider = gtk_combo_box_get_active_text(GTK_COMBO_BOX(map_provider)); #endif for (i = OSM_GPS_MAP_SOURCE_OPENSTREETMAP; i <= OSM_GPS_MAP_SOURCE_YAHOO_STREET; i++) - if (!strcmp(provider,osm_gps_map_source_get_friendly_name(i))) { + if (!strcmp(provider,osm_gps_map_source_get_friendly_name((OsmGpsMapSource_t)i))) { prefs.map_provider = i; break; } @@ -1097,7 +1143,7 @@ static void preferences_dialog(GtkWidget *w, gpointer data) static void create_toggle(const char* label, int *on, void *_data) { - GtkWidget *button, *table = _data; + GtkWidget *button, *table = GTK_WIDGET(_data); int rows, cols, x, y; static int count; @@ -1413,7 +1459,7 @@ static void edit_dc_delete_rows(GtkTreeView *view) selected_rows = gtk_tree_selection_get_selected_rows(selection, &model); for (list = selected_rows; list; list = g_list_next(list)) { - path = list->data; + path = (GtkTreePath *)list->data; ref = gtk_tree_row_reference_new(model, path); row_references = g_list_append(row_references, ref); } @@ -1509,7 +1555,7 @@ static void edit_dc_nicknames(GtkWidget *w, gpointer data) dialog = gtk_dialog_new_with_buttons(_("Edit Dive Computer Nicknames"), GTK_WINDOW(main_window), - GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_DELETE, SUB_RESPONSE_DELETE, GTK_STOCK_CANCEL, @@ -1577,7 +1623,7 @@ static void edit_dc_nicknames(GtkWidget *w, gpointer data) if (res == SUB_RESPONSE_DELETE) { confirm = gtk_dialog_new_with_buttons(_("Delete a dive computer information entry"), GTK_WINDOW(dialog), - GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT, + GtkDialogFlags(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT), GTK_STOCK_YES, GTK_RESPONSE_YES, GTK_STOCK_NO, @@ -1773,8 +1819,143 @@ static gboolean notebook_tooltip (GtkWidget *widget, gint x, gint y, } } +#if NEEDS_TO_MOVE_TO_QT_UI +/* this appears to have moved - but it's very different in qt-ui */ + +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 = 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 = -1; + 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; +} +#endif /* NEEDS_TO_MOVE_TO_QT_UI */ + +void init_qt_ui(int *argcp, char ***argvp) +{ + application->installTranslator(new Translator(application)); + MainWindow *window = new MainWindow(); + window->show(); +} + void init_ui(int *argcp, char ***argvp) { + application = new QApplication(*argcp, *argvp); + +#if QT_VERSION < 0x050000 + // ask QString in Qt 4 to interpret all char* as UTF-8, + // like Qt 5 does. + // 106 is "UTF-8", this is faster than lookup by name + // [http://www.iana.org/assignments/character-sets/character-sets.xml] + QTextCodec::setCodecForCStrings(QTextCodec::codecForMib(106)); +#endif + GtkWidget *win; GtkWidget *nb_page; GtkWidget *dive_list; @@ -1900,11 +2081,12 @@ void init_ui(int *argcp, char ***argvp) void run_ui(void) { - gtk_main(); + application->exec(); } void exit_ui(void) { + delete application; subsurface_close_conf(); if (existing_filename) free((void *)existing_filename); @@ -1924,7 +2106,8 @@ static int tooltips; void attach_tooltip(int x, int y, int w, int h, const char *text, struct event *event) { cairo_rectangle_t *rect; - tooltip_rects = realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t)); + tooltip_rects = (tooltip_record_t *) + realloc(tooltip_rects, (tooltips + 1) * sizeof(tooltip_record_t)); rect = &tooltip_rects[tooltips].rect; rect->x = x; rect->y = y; @@ -2044,7 +2227,7 @@ static gboolean draw_callback(GtkWidget *widget, cairo_t *cr, gpointer data) static gboolean expose_event(GtkWidget *widget, GdkEventExpose *event, gpointer data) { GtkAllocation allocation; - static struct graphics_context gc = { .printer = 0 }; + static struct graphics_context gc = { 0 }; /* the drawing area gives TOTAL width * height - x,y is used as the topx/topy offset * so effective drawing area is width-2x * height-2y */ @@ -2090,7 +2273,7 @@ static gboolean scroll_event(GtkWidget *widget, GdkEventScroll *event, gpointer static void add_gas_change_cb(GtkWidget *menuitem, gpointer data) { - double *x = data; + double *x = (double *)data; int when = x_to_time(*x); int cylnr = select_cylinder(current_dive, when); if (cylnr >= 0) { @@ -2130,7 +2313,7 @@ int confirm_dialog(int when, char *action_text, char *event_text) static void add_bookmark_cb(GtkWidget *menuitem, gpointer data) { - double *x = data; + double *x = (double *)data; int when = x_to_time(*x); if (confirm_dialog(when, _("Add"), _("bookmark"))){ @@ -2158,7 +2341,7 @@ static struct event *event_at_x(double rel_x) static void remove_event_cb(GtkWidget *menuitem, gpointer data) { - struct event *event = data; + struct event *event = (struct event *)data; if (confirm_dialog(event->time.seconds, _("Remove"), _(event->name))){ struct event **ep = ¤t_dc->events; while (ep && *ep != event) @@ -2430,3 +2613,5 @@ gdouble get_screen_dpi(void) gdouble dpi_h = floor((h / h_mm) * mm_per_inch); return dpi_h; } + +#include "qt-gui.moc" |