Freeciv-3.3
Loading...
Searching...
No Matches
mpgui_qt.cpp
Go to the documentation of this file.
1/***********************************************************************
2 Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12***********************************************************************/
13
14#ifdef HAVE_CONFIG_H
15#include <fc_config.h>
16#endif
17
18// Qt
19#include <QApplication>
20#include <QCloseEvent>
21#include <QHBoxLayout>
22#include <QHeaderView>
23#include <QLabel>
24#include <QLineEdit>
25#include <QMainWindow>
26#include <QMessageBox>
27#include <QProgressBar>
28#include <QPushButton>
29#include <QTableWidget>
30#include <QVBoxLayout>
31
32// utility
33#include "fc_cmdline.h"
34#include "fciconv.h"
35#include "fcintl.h"
36#include "log.h"
37
38// common
39#include "version.h"
40
41// tools
42#include "download.h"
43#include "mpcmdline.h"
44#include "mpdb.h"
45#include "mpgui_qt_worker.h"
46#include "modinst.h"
47
48#include "mpgui_qt.h"
49
50struct fcmp_params fcmp = { MODPACK_LIST_URL, nullptr, nullptr };
51
52static mpgui *gui;
53
54static mpqt_worker *worker = nullptr;
55
56static int mpcount = 0;
57
58#define ML_COL_NAME 0
59#define ML_COL_VER 1
60#define ML_COL_INST 2
61#define ML_COL_TYPE 3
62#define ML_COL_SUBTYPE 4
63#define ML_COL_LIC 5
64#define ML_COL_URL 6
65
66#define ML_TYPE 7
67
68#define ML_COL_COUNT 8
69
70static void setup_modpack_list(const char *name, const char *URL,
71 const char *version, const char *license,
72 enum modpack_type type, const char *subtype,
73 const char *notes);
74static void msg_callback(const char *msg);
75static void msg_callback_thr(const char *msg);
76static void progress_callback_thr(int downloaded, int max);
77
78static void gui_download_modpack(QString URL);
79
80/**********************************************************************/
83int main(int argc, char **argv)
84{
85 int ui_options;
86
87 fcmp_init();
88
89 // This modifies argv!
91
92 if (ui_options != -1) {
93 int i;
94
95 for (i = 1; i <= ui_options; i++) {
96 if (is_option("--help", argv[i])) {
98 _("This modpack installer accepts the standard Qt command-line options\n"
99 "after '--'. See the Qt documentation.\n\n"));
100
101 // TRANS: No full stop after the URL, could cause confusion.
102 fc_fprintf(stderr, _("Report bugs at %s\n"), BUG_URL);
103
104 ui_options = -1;
105 }
106 }
107 }
108
109 if (ui_options != -1) {
112 QWidget *central;
113 const char *errmsg;
114 QPixmap *pm;
115 const char *full_icon_path = fileinfoname(get_data_dirs(), "freeciv-modpack.png");
117
119
121 central = new QWidget;
122 main_window = new mpgui_main(qapp, central);
123
124 pm = new QPixmap;
125 pm->load(fip);
126 qapp->setWindowIcon(QIcon(*pm));
127
128 main_window->setGeometry(0, 30, 640, 60);
129 main_window->setWindowTitle(QString::fromUtf8(_("Freeciv modpack installer (Qt)")));
130
131 gui = new mpgui;
132
133 gui->setup(central, &fcmp);
134
135 main_window->setCentralWidget(central);
136 main_window->setVisible(true);
137
139 if (errmsg != nullptr) {
141 }
142
143 qapp->exec();
144
145 if (worker != nullptr) {
146 if (worker->isRunning()) {
147 worker->wait();
148 }
149 delete worker;
150 }
151
152 delete gui;
153 delete qapp;
154
155 close_mpdbs();
156 }
157
158 fcmp_deinit();
160
161 return EXIT_SUCCESS;
162}
163
164/**********************************************************************/
167static void msg_callback(const char *msg)
168{
169 gui->display_msg(msg);
170}
171
172/**********************************************************************/
175static void msg_callback_thr(const char *msg)
176{
177 gui->display_msg_thr(msg);
178}
179
180/**********************************************************************/
183static void progress_callback_thr(int downloaded, int max)
184{
186}
187
188/**********************************************************************/
191void mpgui::setup(QWidget *central, struct fcmp_params *params)
192{
193#define URL_LABEL_TEXT N_("Modpack URL")
194 QVBoxLayout *main_layout = new QVBoxLayout();
195 QHBoxLayout *hl = new QHBoxLayout();
196 QPushButton *install_button = new QPushButton(QString::fromUtf8(_("Install modpack")));
198 QLabel *URL_label;
199 QLabel *version_label;
200 char verbuf[2048];
201 const char *rev_ver;
202 const char *mode;
203
205
206#ifdef FC_QT5_MODE
207 mode = R__("built in Qt5 mode.");
208#elif defined(FC_QT6X_MODE)
209 mode = R__("built in Qt6x mode.");
210#else // FC_QT5_MODE
211 mode = R__("built in Qt6 mode.");
212#endif // FC_QT5_MODE
213
214 if (rev_ver == nullptr) {
215 fc_snprintf(verbuf, sizeof(verbuf), "%s%s\n%s", word_version(),
216 VERSION_STRING, mode);
217 } else {
218 fc_snprintf(verbuf, sizeof(verbuf), _("%s%s\ncommit: %s\n%s"),
220 }
221
222 version_label = new QLabel(QString::fromUtf8(verbuf));
223 version_label->setAlignment(Qt::AlignHCenter);
224 version_label->setParent(central);
225 version_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
226 main_layout->addWidget(version_label);
227
228 mplist_table = new QTableWidget();
229 mplist_table->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
230 mplist_table->setColumnCount(ML_COL_COUNT);
231 headers << QString::fromUtf8(_("Name")) << QString::fromUtf8(_("Version"));
232 headers << QString::fromUtf8(_("Installed")) << QString::fromUtf8(Q_("?modpack:Type"));
233 headers << QString::fromUtf8(_("Subtype")) << QString::fromUtf8(_("License"));
234 headers << QString::fromUtf8(_("URL")) << "typeint";
235 mplist_table->setHorizontalHeaderLabels(headers);
236 mplist_table->verticalHeader()->setVisible(false);
237 mplist_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
238 mplist_table->setSelectionBehavior(QAbstractItemView::SelectRows);
239 mplist_table->setSelectionMode(QAbstractItemView::SingleSelection);
240 mplist_table->hideColumn(ML_TYPE);
241
242 connect(mplist_table, SIGNAL(cellClicked(int, int)), this, SLOT(row_selected(int, int)));
245 connect(this, SIGNAL(progress_thr_signal(int, int)), this, SLOT(progress(int, int)));
247
248 main_layout->addWidget(mplist_table);
249
250 URL_label = new QLabel(QString::fromUtf8(_(URL_LABEL_TEXT)));
251 URL_label->setParent(central);
252 hl->addWidget(URL_label);
253
254 URLedit = new QLineEdit(central);
255 if (params->autoinstall == nullptr) {
256 URLedit->setText(DEFAULT_URL_START);
257 } else {
258 URLedit->setText(QString::fromUtf8(params->autoinstall));
259 }
260 URLedit->setFocus();
261
262 connect(URLedit, SIGNAL(returnPressed()), this, SLOT(URL_given()));
263
264 hl->addWidget(URLedit);
265 main_layout->addLayout(hl);
266
267 connect(install_button, SIGNAL(pressed()), this, SLOT(URL_given()));
268 main_layout->addWidget(install_button);
269
270 bar = new QProgressBar(central);
271 main_layout->addWidget(bar);
272
273 msg_dspl = new QLabel(QString::fromUtf8(_("Select modpack to install")));
274 msg_dspl->setParent(central);
275 main_layout->addWidget(msg_dspl);
276
277 msg_dspl->setAlignment(Qt::AlignHCenter);
278
279 central->setLayout(main_layout);
280}
281
282/**********************************************************************/
286{
287 QByteArray msg_bytes = msg.toLocal8Bit();
288
289 log_verbose("%s", msg_bytes.data());
290 msg_dspl->setText(msg);
291}
292
293/**********************************************************************/
296void mpgui::display_msg_thr(const char *msg)
297{
298 emit display_msg_thr_signal(QString::fromUtf8(msg));
299}
300
301/**********************************************************************/
304void mpgui::progress(int downloaded, int max)
305{
306 bar->setMaximum(max);
307 bar->setValue(downloaded);
308}
309
310/**********************************************************************/
314{
316}
317
318/**********************************************************************/
322{
323 if (worker != nullptr) {
324 if (worker->isRunning()) {
325 gui->display_msg(_("Another download already active"));
326 return;
327 }
328 } else {
329 worker = new mpqt_worker;
330 }
331
333}
334
335/**********************************************************************/
339{
341}
342
343/**********************************************************************/
347{
348 for (int i = 0; i < mpcount; i++) {
350 int type_int;
351 const char *new_inst;
352 enum modpack_type type;
354
355 name_str = mplist_table->item(i, ML_COL_NAME)->text();
356 type_int = mplist_table->item(i, ML_TYPE)->text().toInt();
357 type = (enum modpack_type) type_int;
358 name_bytes = name_str.toUtf8();
360
361 if (new_inst == nullptr) {
362 new_inst = _("Not installed");
363 }
364
365 mplist_table->item(i, ML_COL_INST)->setText(QString::fromUtf8(new_inst));
366 }
367
368 mplist_table->resizeColumnsToContents();
369}
370
371/**********************************************************************/
378
379/**********************************************************************/
382void mpgui::setup_list(const char *name, const char *URL,
383 const char *version, const char *license,
384 enum modpack_type type, const char *subtype,
385 const char *notes)
386{
387 const char *type_str;
388 const char *lic_str;
389 const char *inst_str;
392
395 } else {
396 // TRANS: Unknown modpack type
397 type_str = _("?");
398 }
399
400 if (license != nullptr) {
402 } else {
403 // TRANS: License of modpack is not known
404 lic_str = Q_("?license:Unknown");
405 }
406
408 if (inst_str == nullptr) {
409 inst_str = _("Not installed");
410 }
411
412 mplist_table->setRowCount(mpcount+1);
413
414 item = new QTableWidgetItem(QString::fromUtf8(name));
415 item->setToolTip(QString::fromUtf8(notes));
417 item = new QTableWidgetItem(QString::fromUtf8(version));
418 item->setToolTip(QString::fromUtf8(notes));
420 item = new QTableWidgetItem(QString::fromUtf8(inst_str));
421 item->setToolTip(QString::fromUtf8(notes));
423 item = new QTableWidgetItem(QString::fromUtf8(type_str));
424 item->setToolTip(QString::fromUtf8(notes));
426 item = new QTableWidgetItem(QString::fromUtf8(subtype));
427 item->setToolTip(QString::fromUtf8(notes));
429 item = new QTableWidgetItem(QString::fromUtf8(lic_str));
430 item->setToolTip(QString::fromUtf8(notes));
432 item = new QTableWidgetItem(QString::fromUtf8(URL));
433 item->setToolTip(QString::fromUtf8(notes));
435
436 type_nbr.setNum(type);
438 item->setToolTip(notes);
439 mplist_table->setItem(mpcount, ML_TYPE, item);
440
441 mplist_table->resizeColumnsToContents();
442 mpcount++;
443}
444
445/**********************************************************************/
448static void setup_modpack_list(const char *name, const char *URL,
449 const char *version, const char *license,
450 enum modpack_type type, const char *subtype,
451 const char *notes)
452{
453 // Just call setup_list for gui singleton
454 gui->setup_list(name, URL, version, license, type, subtype, notes);
455}
456
457/**********************************************************************/
461{
462 QString URL = mplist_table->item(row, ML_COL_URL)->text();
463
464 URLedit->setText(URL);
465}
466
467/**********************************************************************/
471{
472 QString URL = mplist_table->item(index.row(), ML_COL_URL)->text();
473
474 URLedit->setText(URL);
475
476 URL_given();
477}
478
479/**********************************************************************/
483{
484 qapp = qapp_in;
486}
487
488/**********************************************************************/
492{
493 QMessageBox ask(central);
494 int ret;
495
496 ask.setText(_("Modpack installation in progress.\nAre you sure you want to quit?"));
497 ask.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
498 ask.setDefaultButton(QMessageBox::Cancel);
499 ask.setIcon(QMessageBox::Warning);
500 ask.setWindowTitle(_("Quit?"));
501 ret = ask.exec();
502
503 switch (ret) {
504 case QMessageBox::Cancel:
505 return;
506 break;
507 case QMessageBox::Ok:
508 qapp->quit();
509 break;
510 }
511}
512
513/**********************************************************************/
517{
518 if (worker != nullptr && worker->isRunning()) {
519 // Download in progress. Confirm quit from user.
521 event->ignore();
522 }
523}
void popup_quit_dialog()
Definition mpgui_qt.cpp:491
QApplication * qapp
Definition mpgui_qt.h:42
mpgui_main(QApplication *qapp_in, QWidget *central_in)
Definition mpgui_qt.cpp:482
QWidget * central
Definition mpgui_qt.h:43
void closeEvent(QCloseEvent *event)
Definition mpgui_qt.cpp:516
void refresh_list_versions_thr()
Definition mpgui_qt.cpp:374
void row_selected(int, int)
Definition mpgui_qt.cpp:460
QLineEdit * URLedit
Definition mpgui_qt.h:79
void display_msg(QString msg)
Definition mpgui_qt.cpp:285
void row_download(const QModelIndex &)
Definition mpgui_qt.cpp:470
void display_msg_thr_signal(QString msg)
void display_msg_thr(const char *msg)
Definition mpgui_qt.cpp:296
void refresh_list_versions_thr_signal()
void URL_given()
Definition mpgui_qt.cpp:338
void progress_thr_signal(int downloaded, int max)
QTableWidget * mplist_table
Definition mpgui_qt.h:82
void progress_thr(int downloaded, int max)
Definition mpgui_qt.cpp:313
void setup_list(const char *name, const char *URL, const char *version, const char *license, enum modpack_type type, const char *subtype, const char *notes)
Definition mpgui_qt.cpp:382
QLabel * msg_dspl
Definition mpgui_qt.h:80
void refresh_list_versions()
Definition mpgui_qt.cpp:346
QProgressBar * bar
Definition mpgui_qt.h:81
void setup(QWidget *central, struct fcmp_params *fcmp)
Definition mpgui_qt.cpp:191
void progress(int downloaded, int max)
Definition mpgui_qt.cpp:304
void download(QString URL_in, class mpgui *gui_in, struct fcmp_params *fcmp_in, dl_msg_callback msg_callback_in, dl_pb_callback pb_callback_in)
char * incite_cost
Definition comments.c:76
const char * download_modpack_list(const struct fcmp_params *fcmp, modpack_list_setup_cb cb, dl_msg_callback mcb)
Definition download.c:427
enum event_type event
Definition events.c:81
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
#define Q_(String)
Definition fcintl.h:70
#define _(String)
Definition fcintl.h:67
#define R__(String)
Definition fcintl.h:75
GType type
Definition repodlgs.c:1313
static QApplication * qapp
Definition gui_main.cpp:69
const char * name
Definition inputfile.c:127
#define log_verbose(message,...)
Definition log.h:110
void load_install_info_lists(struct fcmp_params *fcmp)
Definition modinst.c:44
void fcmp_deinit(void)
Definition modinst.c:98
void fcmp_init(void)
Definition modinst.c:82
#define DEFAULT_URL_START
Definition modinst.h:32
#define MODPACK_LIST_URL
Definition modinst.h:30
int fcmp_parse_cmdline(int argc, char *argv[])
Definition mpcmdline.c:45
const char * mpdb_installed_version(const char *name, enum modpack_type type)
Definition mpdb.c:253
void close_mpdbs(void)
Definition mpdb.c:202
#define ML_COL_SUBTYPE
Definition mpgui_gtk3.c:58
#define ML_TYPE
Definition mpgui_gtk3.c:64
#define ML_COL_NAME
Definition mpgui_gtk3.c:54
#define ML_COL_LIC
Definition mpgui_gtk3.c:59
static void gui_download_modpack(const char *URL)
Definition mpgui_gtk3.c:279
#define ML_COL_TYPE
Definition mpgui_gtk3.c:57
#define ML_COL_INST
Definition mpgui_gtk3.c:56
#define ML_COL_URL
Definition mpgui_gtk3.c:60
#define ML_COL_VER
Definition mpgui_gtk3.c:55
#define ML_COL_COUNT
Definition mpgui_gtk3.c:62
static mpgui * gui
Definition mpgui_qt.cpp:52
static int mpcount
Definition mpgui_qt.cpp:56
static void progress_callback_thr(int downloaded, int max)
Definition mpgui_qt.cpp:183
int main(int argc, char **argv)
Definition mpgui_qt.cpp:83
static void msg_callback_thr(const char *msg)
Definition mpgui_qt.cpp:175
static void gui_download_modpack(QString URL)
Definition mpgui_qt.cpp:321
#define URL_LABEL_TEXT
struct fcmp_params fcmp
Definition mpgui_qt.cpp:50
static mpqt_worker * worker
Definition mpgui_qt.cpp:54
static void setup_modpack_list(const char *name, const char *URL, const char *version, const char *license, enum modpack_type type, const char *subtype, const char *notes)
Definition mpgui_qt.cpp:448
static void msg_callback(const char *msg)
Definition mpgui_qt.cpp:167
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1094
const struct strvec * get_data_dirs(void)
Definition shared.c:886
const char * autoinstall
Definition modinst.h:20
Definition climisc.h:82
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:960
const char * fc_git_revision(void)
Definition version.c:75
const char * word_version(void)
Definition version.c:62