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 
19 //Qt
20 #include <QApplication>
21 #include <QGridLayout>
22 #include <QLabel>
23 #include <QPushButton>
24 #include <QTableWidget>
25 #include <QHeaderView>
26 
27 // client
28 #include "options.h"
29 
30 // gui-qt
31 #include "fc_client.h"
32 #include "messagedlg.h"
33 
34 extern QApplication *qapp;
35 /**************************************************************************
36   Message widget constructor
37 **************************************************************************/
message_dlg()38 message_dlg::message_dlg()
39 {
40   int index;
41   QStringList slist;
42   QLabel *empty1, *empty2;
43   QPushButton *but1;
44   QPushButton *but2;
45   QMargins margins;
46   int len;
47 
48   setAttribute(Qt::WA_DeleteOnClose);
49   empty1 = new QLabel;
50   empty2 = new QLabel;
51   layout = new QGridLayout;
52   msgtab = new QTableWidget;
53   slist << _("Event") << _("Out") << _("Mes") << _("Pop");
54   msgtab->setColumnCount(slist.count());
55   msgtab->setHorizontalHeaderLabels(slist);
56   msgtab->setProperty("showGrid", "false");
57   msgtab->setEditTriggers(QAbstractItemView::NoEditTriggers);
58   msgtab->horizontalHeader()->resizeSections(QHeaderView::
59                                              ResizeToContents);
60   msgtab->verticalHeader()->setVisible(false);
61   msgtab->setSelectionMode(QAbstractItemView::NoSelection);
62   msgtab->setSelectionBehavior(QAbstractItemView::SelectColumns);
63 
64   but1 = new QPushButton(style()->standardIcon(
65                          QStyle::SP_DialogCancelButton), _("Cancel"));
66   connect(but1, &QAbstractButton::clicked, this, &message_dlg::cancel_changes);
67   layout->addWidget(but1, 1, 1, 1, 1);
68   but2 = new QPushButton(style()->standardIcon(QStyle::SP_DialogOkButton),
69                          _("Ok"));
70   connect(but2, &QAbstractButton::clicked, this, &message_dlg::apply_changes);
71   layout->addWidget(but2, 1, 2, 1, 1, Qt::AlignRight);
72   layout->addWidget(empty1, 0, 0, 1, 1);
73   layout->addWidget(msgtab, 0, 1, 1, 2);
74   layout->addWidget(empty2, 0, 3, 1, 1);
75   layout->setColumnStretch(0, 1);
76   layout->setColumnStretch(1, 10);
77   layout->setColumnStretch(3, 1);
78   setLayout(layout);
79   gui()->gimme_place(this, "MSD");
80   index = gui()->add_game_tab(this);
81   gui()->game_tab_widget->setCurrentIndex(index);
82 
83   fill_data();
84   margins = msgtab->contentsMargins();
85   len = msgtab->horizontalHeader()->length() + margins.left()
86         + margins.right()
87         + qapp->style()->pixelMetric(QStyle::PM_ScrollBarExtent);
88   msgtab->setFixedWidth(len);
89   msgtab->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
90   but1->setFixedWidth(len / 3);
91   but2->setFixedWidth(len / 3);
92 }
93 
94 /**************************************************************************
95   Message widget destructor
96 **************************************************************************/
~message_dlg()97 message_dlg::~message_dlg()
98 {
99   gui()->remove_repo_dlg("MSD");
100 }
101 
102 /**************************************************************************
103   Fills column in table
104 **************************************************************************/
fill_data()105 void message_dlg::fill_data()
106 {
107   int i, j;
108   QTableWidgetItem *item;
109   i = 0;
110   msgtab->setRowCount(0);
111 
112   sorted_event_iterate(ev) {
113     item =  new QTableWidgetItem;
114     item->setText(get_event_message_text(ev));
115     msgtab->insertRow(i);
116     msgtab->setItem(i, 0, item);
117     for (j = 0; j < NUM_MW; j++) {
118       bool checked;
119       item =  new QTableWidgetItem;
120       checked =  messages_where[ev] & (1 << j);
121       if (checked) {
122         item->setCheckState(Qt::Checked);
123       } else {
124         item->setCheckState(Qt::Unchecked);
125       }
126       msgtab->setItem(i, j + 1, item);
127     }
128     i++;
129   } sorted_event_iterate_end;
130   msgtab->resizeColumnsToContents();
131 
132 }
133 
134 /**************************************************************************
135   Apply changes and closes widget
136 **************************************************************************/
apply_changes()137 void message_dlg::apply_changes()
138 {
139   int i, j;
140   QTableWidgetItem *item;
141   Qt::CheckState state;
142   for (i = 0; i <= event_type_max(); i++) {
143     /* Include possible undefined messages. */
144     messages_where[i] = 0;
145   }
146   i = 0;
147   sorted_event_iterate(ev) {
148     for (j = 0; j < NUM_MW; j++) {
149       bool checked;
150       item = msgtab->item(i, j + 1);
151       checked =  messages_where[ev] & (1 << j);
152       state = item->checkState();
153       if ((state == Qt::Checked && !checked)
154           || (state == Qt::Unchecked && checked)) {
155         messages_where[ev] |= (1 << j);
156       }
157     }
158     i++;
159   } sorted_event_iterate_end;
160   close();
161 }
162 
163 /**************************************************************************
164   Closes widget
165 **************************************************************************/
cancel_changes()166 void message_dlg::cancel_changes()
167 {
168   close();
169 }
170 
171 /**************************************************************************
172   Popup a window to let the user edit their message options.
173 **************************************************************************/
popup_messageopt_dialog(void)174 void popup_messageopt_dialog(void)
175 {
176   message_dlg *mdlg;
177   int i;
178   QWidget *w;
179 
180   if (!gui()->is_repo_dlg_open("MSD")) {
181     mdlg = new message_dlg;
182   } else {
183     i = gui()->gimme_index_of("MSD");
184     fc_assert(i != -1);
185     if (gui()->game_tab_widget->currentIndex() == i) {
186       return;
187     }
188     w = gui()->game_tab_widget->widget(i);
189     mdlg = reinterpret_cast<message_dlg *>(w);
190     gui()->game_tab_widget->setCurrentWidget(mdlg);
191   }
192 }
193