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
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#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"),
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)));
243 connect(this, SIGNAL(progress_thr_signal(int, int)), this, SLOT(progress(int, int)));
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/**********************************************************************/
284{
285 QByteArray msg_bytes = msg.toLocal8Bit();
286
287 log_verbose("%s", msg_bytes.data());
288 msg_dspl->setText(msg);
289}
290
291/**********************************************************************/
294void mpgui::display_msg_thr(const char *msg)
295{
296 emit display_msg_thr_signal(QString::fromUtf8(msg));
297}
298
299/**********************************************************************/
302void mpgui::progress(int downloaded, int max)
303{
304 bar->setMaximum(max);
305 bar->setValue(downloaded);
306}
307
308/**********************************************************************/
312{
314}
315
316/**********************************************************************/
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
331}
332
333/**********************************************************************/
337{
339}
340
341/**********************************************************************/
345{
346 for (int i = 0; i < mpcount; i++) {
348 int type_int;
349 const char *new_inst;
350 enum modpack_type type;
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();
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/**********************************************************************/
376
377/**********************************************************************/
380void 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;
390
393 } else {
394 // TRANS: Unknown modpack type
395 type_str = _("?");
396 }
397
398 if (license != nullptr) {
400 } else {
401 // TRANS: License of modpack is not known
402 lic_str = Q_("?license:Unknown");
403 }
404
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));
415 item = new QTableWidgetItem(QString::fromUtf8(version));
416 item->setToolTip(QString::fromUtf8(notes));
418 item = new QTableWidgetItem(QString::fromUtf8(inst_str));
419 item->setToolTip(QString::fromUtf8(notes));
421 item = new QTableWidgetItem(QString::fromUtf8(type_str));
422 item->setToolTip(QString::fromUtf8(notes));
424 item = new QTableWidgetItem(QString::fromUtf8(subtype));
425 item->setToolTip(QString::fromUtf8(notes));
427 item = new QTableWidgetItem(QString::fromUtf8(lic_str));
428 item->setToolTip(QString::fromUtf8(notes));
430 item = new QTableWidgetItem(QString::fromUtf8(URL));
431 item->setToolTip(QString::fromUtf8(notes));
433
434 type_nbr.setNum(type);
436 item->setToolTip(notes);
437 mplist_table->setItem(mpcount, ML_TYPE, item);
438
439 mplist_table->resizeColumnsToContents();
440 mpcount++;
441}
442
443/**********************************************************************/
446static 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/**********************************************************************/
459{
460 QString URL = mplist_table->item(row, ML_COL_URL)->text();
461
462 URLedit->setText(URL);
463}
464
465/**********************************************************************/
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/**********************************************************************/
481{
482 qapp = qapp_in;
484}
485
486/**********************************************************************/
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/**********************************************************************/
515{
516 if (worker != nullptr && worker->isRunning()) {
517 // Download in progress. Confirm quit from user.
519 event->ignore();
520 }
521}
void popup_quit_dialog()
Definition mpgui_qt.cpp:489
QApplication * qapp
Definition mpgui_qt.h:42
mpgui_main(QApplication *qapp_in, QWidget *central_in)
Definition mpgui_qt.cpp:480
QWidget * central
Definition mpgui_qt.h:43
void closeEvent(QCloseEvent *event)
Definition mpgui_qt.cpp:514
void refresh_list_versions_thr()
Definition mpgui_qt.cpp:372
void row_selected(int, int)
Definition mpgui_qt.cpp:458
QLineEdit * URLedit
Definition mpgui_qt.h:79
void display_msg(QString msg)
Definition mpgui_qt.cpp:283
void row_download(const QModelIndex &)
Definition mpgui_qt.cpp:468
void display_msg_thr_signal(QString msg)
void display_msg_thr(const char *msg)
Definition mpgui_qt.cpp:294
void refresh_list_versions_thr_signal()
void URL_given()
Definition mpgui_qt.cpp:336
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:311
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
QLabel * msg_dspl
Definition mpgui_qt.h:80
void refresh_list_versions()
Definition mpgui_qt.cpp:344
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:302
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:75
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:71
const char * name
Definition inputfile.c:127
#define log_verbose(message,...)
Definition log.h:109
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:319
#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:446
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:1101
const struct strvec * get_data_dirs(void)
Definition shared.c:893
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:974
const char * fc_git_revision(void)
Definition version.c:75
const char * word_version(void)
Definition version.c:62