Freeciv-3.2: /fast/freeciv/nightly/S3_2/tools/fcmp/mpgui_qt.cpp Source File

Freeciv-3.2

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
50 struct fcmp_params fcmp = { MODPACK_LIST_URL, NULL, NULL };
51
52 static mpgui *gui;
53
54 static mpqt_worker *worker = nullptr;
55
56 static 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/**********************************************************************/
83 int main(int argc, char **argv)
84{
85 int ui_options;
86
87 fcmp_init();
88
89 // This modifies argv!
90 ui_options = fcmp_parse_cmdline(argc, 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])) {
97 fc_fprintf(stderr,
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) {
110 QApplication *qapp;
111 mpgui_main *main_window;
112 QWidget *central;
113 const char *errmsg;
114 QPixmap *pm;
115 const char *full_icon_path = fileinfoname(get_data_dirs(), "freeciv-modpack.png");
116 const QString fip = QString(full_icon_path);
117
118 load_install_info_lists(&fcmp);
119
120 qapp = new QApplication(ui_options, argv);
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
138 errmsg = download_modpack_list(&fcmp, setup_modpack_list, msg_callback);
139 if (errmsg != nullptr) {
140 gui->display_msg(errmsg);
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();
159 cmdline_option_values_free();
160
161 return EXIT_SUCCESS;
162}
163
164/**********************************************************************/
167 static void msg_callback(const char *msg)
168{
169 gui->display_msg(msg);
170}
171
172/**********************************************************************/
175 static void msg_callback_thr(const char *msg)
176{
177 gui->display_msg_thr(msg);
178}
179
180/**********************************************************************/
183 static void progress_callback_thr(int downloaded, int max)
184{
185 gui->progress_thr(downloaded, max);
186}
187
188/**********************************************************************/
191 void 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")));
197 QStringList headers;
198 QLabel *URL_label;
199 QLabel *version_label;
200 char verbuf[2048];
201 const char *rev_ver;
202 const char *mode;
203
204 rev_ver = fc_git_revision();
205
206#ifndef FC_QT5_MODE
207 mode = R__("built in Qt6 mode.");
208#else // FC_QT5_MODE
209 mode = R__("built in Qt5 mode.");
210#endif // FC_QT5_MODE
211
212 if (rev_ver == nullptr) {
213 fc_snprintf(verbuf, sizeof(verbuf), "%s%s\n%s", word_version(),
214 VERSION_STRING, mode);
215 } else {
216 fc_snprintf(verbuf, sizeof(verbuf), _("%s%s\ncommit: %s\n%s"),
217 word_version(), VERSION_STRING, rev_ver, mode);
218 }
219
220 version_label = new QLabel(QString::fromUtf8(verbuf));
221 version_label->setAlignment(Qt::AlignHCenter);
222 version_label->setParent(central);
223 version_label->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
224 main_layout->addWidget(version_label);
225
226 mplist_table = new QTableWidget();
227 mplist_table->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
228 mplist_table->setColumnCount(ML_COL_COUNT);
229 headers << QString::fromUtf8(_("Name")) << QString::fromUtf8(_("Version"));
230 headers << QString::fromUtf8(_("Installed")) << QString::fromUtf8(Q_("?modpack:Type"));
231 headers << QString::fromUtf8(_("Subtype")) << QString::fromUtf8(_("License"));
232 headers << QString::fromUtf8(_("URL")) << "typeint";
233 mplist_table->setHorizontalHeaderLabels(headers);
234 mplist_table->verticalHeader()->setVisible(false);
235 mplist_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
236 mplist_table->setSelectionBehavior(QAbstractItemView::SelectRows);
237 mplist_table->setSelectionMode(QAbstractItemView::SingleSelection);
238 mplist_table->hideColumn(ML_TYPE);
239
240 connect(mplist_table, SIGNAL(cellClicked(int, int)), this, SLOT(row_selected(int, int)));
241 connect(mplist_table, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(row_download(QModelIndex)));
242 connect(this, SIGNAL(display_msg_thr_signal(QString)), this, SLOT(display_msg(QString)));
243 connect(this, SIGNAL(progress_thr_signal(int, int)), this, SLOT(progress(int, int)));
244 connect(this, SIGNAL(refresh_list_versions_thr_signal()), this, SLOT(refresh_list_versions()));
245
246 main_layout->addWidget(mplist_table);
247
248 URL_label = new QLabel(QString::fromUtf8(_(URL_LABEL_TEXT)));
249 URL_label->setParent(central);
250 hl->addWidget(URL_label);
251
252 URLedit = new QLineEdit(central);
253 if (params->autoinstall == nullptr) {
254 URLedit->setText(DEFAULT_URL_START);
255 } else {
256 URLedit->setText(QString::fromUtf8(params->autoinstall));
257 }
258 URLedit->setFocus();
259
260 connect(URLedit, SIGNAL(returnPressed()), this, SLOT(URL_given()));
261
262 hl->addWidget(URLedit);
263 main_layout->addLayout(hl);
264
265 connect(install_button, SIGNAL(pressed()), this, SLOT(URL_given()));
266 main_layout->addWidget(install_button);
267
268 bar = new QProgressBar(central);
269 main_layout->addWidget(bar);
270
271 msg_dspl = new QLabel(QString::fromUtf8(_("Select modpack to install")));
272 msg_dspl->setParent(central);
273 main_layout->addWidget(msg_dspl);
274
275 msg_dspl->setAlignment(Qt::AlignHCenter);
276
277 central->setLayout(main_layout);
278}
279
280/**********************************************************************/
283 void mpgui::display_msg(QString msg)
284{
285 QByteArray msg_bytes = msg.toLocal8Bit();
286
287 log_verbose("%s", msg_bytes.data());
288 msg_dspl->setText(msg);
289}
290
291/**********************************************************************/
294 void mpgui::display_msg_thr(const char *msg)
295{
296 emit display_msg_thr_signal(QString::fromUtf8(msg));
297}
298
299/**********************************************************************/
302 void mpgui::progress(int downloaded, int max)
303{
304 bar->setMaximum(max);
305 bar->setValue(downloaded);
306}
307
308/**********************************************************************/
311 void mpgui::progress_thr(int downloaded, int max)
312{
313 emit progress_thr_signal(downloaded, max);
314}
315
316/**********************************************************************/
319 static void gui_download_modpack(QString URL)
320{
321 if (worker != nullptr) {
322 if (worker->isRunning()) {
323 gui->display_msg(_("Another download already active"));
324 return;
325 }
326 } else {
327 worker = new mpqt_worker;
328 }
329
330 worker->download(URL, gui, &fcmp, msg_callback_thr, progress_callback_thr);
331}
332
333/**********************************************************************/
336 void mpgui::URL_given()
337{
338 gui_download_modpack(URLedit->text());
339}
340
341/**********************************************************************/
344 void mpgui::refresh_list_versions()
345{
346 for (int i = 0; i < mpcount; i++) {
347 QString name_str;
348 int type_int;
349 const char *new_inst;
350 enum modpack_type type;
351 QByteArray name_bytes;
352
353 name_str = mplist_table->item(i, ML_COL_NAME)->text();
354 type_int = mplist_table->item(i, ML_TYPE)->text().toInt();
355 type = (enum modpack_type) type_int;
356 name_bytes = name_str.toUtf8();
357 new_inst = mpdb_installed_version(name_bytes.data(), type);
358
359 if (new_inst == nullptr) {
360 new_inst = _("Not installed");
361 }
362
363 mplist_table->item(i, ML_COL_INST)->setText(QString::fromUtf8(new_inst));
364 }
365
366 mplist_table->resizeColumnsToContents();
367}
368
369/**********************************************************************/
372 void mpgui::refresh_list_versions_thr()
373{
374 emit refresh_list_versions_thr_signal();
375}
376
377/**********************************************************************/
380 void mpgui::setup_list(const char *name, const char *URL,
381 const char *version, const char *license,
382 enum modpack_type type, const char *subtype,
383 const char *notes)
384{
385 const char *type_str;
386 const char *lic_str;
387 const char *inst_str;
388 QString type_nbr;
389 QTableWidgetItem *item;
390
391 if (modpack_type_is_valid(type)) {
392 type_str = _(modpack_type_name(type));
393 } else {
394 // TRANS: Unknown modpack type
395 type_str = _("?");
396 }
397
398 if (license != nullptr) {
399 lic_str = license;
400 } else {
401 // TRANS: License of modpack is not known
402 lic_str = Q_("?license:Unknown");
403 }
404
405 inst_str = mpdb_installed_version(name, type);
406 if (inst_str == nullptr) {
407 inst_str = _("Not installed");
408 }
409
410 mplist_table->setRowCount(mpcount+1);
411
412 item = new QTableWidgetItem(QString::fromUtf8(name));
413 item->setToolTip(QString::fromUtf8(notes));
414 mplist_table->setItem(mpcount, ML_COL_NAME, item);
415 item = new QTableWidgetItem(QString::fromUtf8(version));
416 item->setToolTip(QString::fromUtf8(notes));
417 mplist_table->setItem(mpcount, ML_COL_VER, item);
418 item = new QTableWidgetItem(QString::fromUtf8(inst_str));
419 item->setToolTip(QString::fromUtf8(notes));
420 mplist_table->setItem(mpcount, ML_COL_INST, item);
421 item = new QTableWidgetItem(QString::fromUtf8(type_str));
422 item->setToolTip(QString::fromUtf8(notes));
423 mplist_table->setItem(mpcount, ML_COL_TYPE, item);
424 item = new QTableWidgetItem(QString::fromUtf8(subtype));
425 item->setToolTip(QString::fromUtf8(notes));
426 mplist_table->setItem(mpcount, ML_COL_SUBTYPE, item);
427 item = new QTableWidgetItem(QString::fromUtf8(lic_str));
428 item->setToolTip(QString::fromUtf8(notes));
429 mplist_table->setItem(mpcount, ML_COL_LIC, item);
430 item = new QTableWidgetItem(QString::fromUtf8(URL));
431 item->setToolTip(QString::fromUtf8(notes));
432 mplist_table->setItem(mpcount, ML_COL_URL, item);
433
434 type_nbr.setNum(type);
435 item = new QTableWidgetItem(type_nbr);
436 item->setToolTip(notes);
437 mplist_table->setItem(mpcount, ML_TYPE, item);
438
439 mplist_table->resizeColumnsToContents();
440 mpcount++;
441}
442
443/**********************************************************************/
446 static void setup_modpack_list(const char *name, const char *URL,
447 const char *version, const char *license,
448 enum modpack_type type, const char *subtype,
449 const char *notes)
450{
451 // Just call setup_list for gui singleton
452 gui->setup_list(name, URL, version, license, type, subtype, notes);
453}
454
455/**********************************************************************/
458 void mpgui::row_selected(int row, int column)
459{
460 QString URL = mplist_table->item(row, ML_COL_URL)->text();
461
462 URLedit->setText(URL);
463}
464
465/**********************************************************************/
468 void mpgui::row_download(const QModelIndex &index)
469{
470 QString URL = mplist_table->item(index.row(), ML_COL_URL)->text();
471
472 URLedit->setText(URL);
473
474 URL_given();
475}
476
477/**********************************************************************/
480mpgui_main::mpgui_main(QApplication *qapp_in, QWidget *central_in) : QMainWindow()
481{
482 qapp = qapp_in;
483 central = central_in;
484}
485
486/**********************************************************************/
489 void mpgui_main::popup_quit_dialog()
490{
491 QMessageBox ask(central);
492 int ret;
493
494 ask.setText(_("Modpack installation in progress.\nAre you sure you want to quit?"));
495 ask.setStandardButtons(QMessageBox::Cancel | QMessageBox::Ok);
496 ask.setDefaultButton(QMessageBox::Cancel);
497 ask.setIcon(QMessageBox::Warning);
498 ask.setWindowTitle(_("Quit?"));
499 ret = ask.exec();
500
501 switch (ret) {
502 case QMessageBox::Cancel:
503 return;
504 break;
505 case QMessageBox::Ok:
506 qapp->quit();
507 break;
508 }
509}
510
511/**********************************************************************/
514 void mpgui_main::closeEvent(QCloseEvent *event)
515{
516 if (worker != nullptr && worker->isRunning()) {
517 // Download in progress. Confirm quit from user.
518 popup_quit_dialog();
519 event->ignore();
520 }
521}
mpgui_main
Definition mpgui_qt.h:34
mpgui_main::popup_quit_dialog
void popup_quit_dialog()
Definition mpgui_qt.cpp:489
mpgui_main::qapp
QApplication * qapp
Definition mpgui_qt.h:42
mpgui_main::mpgui_main
mpgui_main(QApplication *qapp_in, QWidget *central_in)
Definition mpgui_qt.cpp:480
mpgui_main::central
QWidget * central
Definition mpgui_qt.h:43
mpgui_main::closeEvent
void closeEvent(QCloseEvent *event)
Definition mpgui_qt.cpp:514
mpgui
Definition mpgui_qt.h:50
mpgui::refresh_list_versions_thr
void refresh_list_versions_thr()
Definition mpgui_qt.cpp:372
mpgui::row_selected
void row_selected(int, int)
Definition mpgui_qt.cpp:458
mpgui::URLedit
QLineEdit * URLedit
Definition mpgui_qt.h:79
mpgui::display_msg
void display_msg(QString msg)
Definition mpgui_qt.cpp:283
mpgui::row_download
void row_download(const QModelIndex &)
Definition mpgui_qt.cpp:468
mpgui::display_msg_thr_signal
void display_msg_thr_signal(QString msg)
mpgui::display_msg_thr
void display_msg_thr(const char *msg)
Definition mpgui_qt.cpp:294
mpgui::refresh_list_versions_thr_signal
void refresh_list_versions_thr_signal()
mpgui::URL_given
void URL_given()
Definition mpgui_qt.cpp:336
mpgui::progress_thr_signal
void progress_thr_signal(int downloaded, int max)
mpgui::mplist_table
QTableWidget * mplist_table
Definition mpgui_qt.h:82
mpgui::progress_thr
void progress_thr(int downloaded, int max)
Definition mpgui_qt.cpp:311
mpgui::setup_list
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:380
mpgui::msg_dspl
QLabel * msg_dspl
Definition mpgui_qt.h:80
mpgui::refresh_list_versions
void refresh_list_versions()
Definition mpgui_qt.cpp:344
mpgui::bar
QProgressBar * bar
Definition mpgui_qt.h:81
mpgui::setup
void setup(QWidget *central, struct fcmp_params *fcmp)
Definition mpgui_qt.cpp:191
mpgui::progress
void progress(int downloaded, int max)
Definition mpgui_qt.cpp:302
mpqt_worker
Definition mpgui_qt_worker.h:32
mpqt_worker::download
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)
Definition mpgui_qt_worker.cpp:63
incite_cost
char * incite_cost
Definition comments.c:74
download_modpack_list
const char * download_modpack_list(const struct fcmp_params *fcmp, modpack_list_setup_cb cb, dl_msg_callback mcb)
Definition download.c:427
download.h
event
enum event_type event
Definition events.c:81
is_option
bool is_option(const char *option_name, char *option)
Definition fc_cmdline.c:112
cmdline_option_values_free
void cmdline_option_values_free(void)
Definition fc_cmdline.c:97
fc_cmdline.h
fciconv.h
fc_fprintf
void fc_fprintf(FILE *stream, const char *format,...) fc__attribute((__format__(__printf__
fcintl.h
Q_
#define Q_(String)
Definition fcintl.h:70
_
#define _(String)
Definition fcintl.h:67
R__
#define R__(String)
Definition fcintl.h:75
type
GType type
Definition repodlgs.c:1313
qapp
static QApplication * qapp
Definition gui_main.cpp:71
name
const char * name
Definition inputfile.c:127
log.h
log_verbose
#define log_verbose(message,...)
Definition log.h:109
load_install_info_lists
void load_install_info_lists(struct fcmp_params *fcmp)
Definition modinst.c:44
fcmp_deinit
void fcmp_deinit(void)
Definition modinst.c:98
fcmp_init
void fcmp_init(void)
Definition modinst.c:82
modinst.h
DEFAULT_URL_START
#define DEFAULT_URL_START
Definition modinst.h:32
MODPACK_LIST_URL
#define MODPACK_LIST_URL
Definition modinst.h:30
fcmp_parse_cmdline
int fcmp_parse_cmdline(int argc, char *argv[])
Definition mpcmdline.c:45
mpcmdline.h
mpdb_installed_version
const char * mpdb_installed_version(const char *name, enum modpack_type type)
Definition mpdb.c:253
close_mpdbs
void close_mpdbs(void)
Definition mpdb.c:202
mpdb.h
ML_COL_SUBTYPE
#define ML_COL_SUBTYPE
Definition mpgui_gtk3.c:58
ML_TYPE
#define ML_TYPE
Definition mpgui_gtk3.c:64
ML_COL_NAME
#define ML_COL_NAME
Definition mpgui_gtk3.c:54
ML_COL_LIC
#define ML_COL_LIC
Definition mpgui_gtk3.c:59
gui_download_modpack
static void gui_download_modpack(const char *URL)
Definition mpgui_gtk3.c:279
ML_COL_TYPE
#define ML_COL_TYPE
Definition mpgui_gtk3.c:57
ML_COL_INST
#define ML_COL_INST
Definition mpgui_gtk3.c:56
ML_COL_URL
#define ML_COL_URL
Definition mpgui_gtk3.c:60
ML_COL_VER
#define ML_COL_VER
Definition mpgui_gtk3.c:55
ML_COL_COUNT
#define ML_COL_COUNT
Definition mpgui_gtk3.c:62
gui
static mpgui * gui
Definition mpgui_qt.cpp:52
mpcount
static int mpcount
Definition mpgui_qt.cpp:56
progress_callback_thr
static void progress_callback_thr(int downloaded, int max)
Definition mpgui_qt.cpp:183
main
int main(int argc, char **argv)
Definition mpgui_qt.cpp:83
msg_callback_thr
static void msg_callback_thr(const char *msg)
Definition mpgui_qt.cpp:175
gui_download_modpack
static void gui_download_modpack(QString URL)
Definition mpgui_qt.cpp:319
URL_LABEL_TEXT
#define URL_LABEL_TEXT
fcmp
struct fcmp_params fcmp
Definition mpgui_qt.cpp:50
worker
static mpqt_worker * worker
Definition mpgui_qt.cpp:54
setup_modpack_list
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:446
msg_callback
static void msg_callback(const char *msg)
Definition mpgui_qt.cpp:167
mpgui_qt.h
mpgui_qt_worker.h
fileinfoname
const char * fileinfoname(const struct strvec *dirs, const char *filename)
Definition shared.c:1101
get_data_dirs
const struct strvec * get_data_dirs(void)
Definition shared.c:893
fcmp_params
Definition modinst.h:17
fcmp_params::autoinstall
const char * autoinstall
Definition modinst.h:20
item
Definition climisc.h:80
fc_snprintf
int fc_snprintf(char *str, size_t n, const char *format,...)
Definition support.c:974
fc_git_revision
const char * fc_git_revision(void)
Definition version.c:75
word_version
const char * word_version(void)
Definition version.c:62
version.h