demometadatadialog.cpp
1 //------------------------------------------------------------------------------
2 // demometadatadialog.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2024 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "demometadatadialog.h"
24 
25 #include "gamedemo.h"
26 #include "gui/commongui.h"
27 #include "plugins/engineplugin.h"
29 
30 #include "ui_demometadatadialog.h"
31 
32 #include <QKeyEvent>
33 #include <QStandardItemModel>
34 
35 DClass<DemoMetaDataDialog> : public Ui::DemoMetaDataDialog
36 {
37 public:
38  GameDemo demo;
39  QStandardItemModel *wadsModel;
40 };
41 
42 DPointeredNoCopy(DemoMetaDataDialog);
43 
44 DemoMetaDataDialog::DemoMetaDataDialog(QWidget *parent, const GameDemo &demo)
45  : QDialog(parent)
46 {
47  d->setupUi(this);
48  d->demo = demo;
49 
50  d->cboGame->setAllowUnknown(true);
51 
52  d->wadsModel = new QStandardItemModel(this);
53  d->wadsList->setModel(d->wadsModel);
54  d->wadsList->installEventFilter(this);
55 
56  d->cboGame->setPluginByName(demo.game);
57  d->leGameVersion->setText(demo.gameVersion);
58  d->leAuthor->setText(demo.author);
59  d->dteCreatedTime->setDateTime(demo.time.isValid() ? demo.time : QDateTime::currentDateTime());
60  d->leIwad->setText(demo.iwad);
61 
62  for (const PWad &wad : demo.wads)
63  addWad(wad);
64 }
65 
66 void DemoMetaDataDialog::addEmptyWad()
67 {
68  addWad(PWad(""));
69 }
70 
71 void DemoMetaDataDialog::addWad(const PWad &wad)
72 {
73  auto it = new QStandardItem(wad.name());
74  it->setDragEnabled(true);
75  it->setCheckable(true);
76  it->setCheckState(wad.isOptional() ? Qt::Unchecked : Qt::Checked);
77  d->wadsModel->appendRow(it);
78 }
79 
80 bool DemoMetaDataDialog::eventFilter(QObject *obj, QEvent *event)
81 {
82  if (obj == d->wadsList)
83  {
84  if (event->type() == QEvent::KeyPress)
85  {
86  QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
87  switch (keyEvent->key())
88  {
89  case Qt::Key_Delete:
90  case Qt::Key_Minus:
91  removeSelectedWads();
92  return true;
93  case Qt::Key_Plus:
94  addEmptyWad();
95  return true;
96  default:
97  // no-op
98  break;
99  }
100  }
101  }
102  return false;
103 }
104 
105 GameDemo DemoMetaDataDialog::gameDemo() const
106 {
107  EnginePlugin *game = d->cboGame->currentPlugin();
108  const QString gameName = game != nullptr
109  ? game->data()->name
110  : d->demo.game;
111 
112  GameDemo demo;
113  demo.demopath = d->demo.demopath;
114  demo.game = gameName;
115  demo.gameVersion = d->leGameVersion->text().trimmed();
116  demo.author = d->leAuthor->text().trimmed();
117  demo.time = d->dteCreatedTime->dateTime();
118  demo.iwad = d->leIwad->text().trimmed();
119  for (int i = 0; i < d->wadsModel->rowCount(); ++i)
120  {
121  auto item = d->wadsModel->item(i);
122  QString filename = item->text().trimmed();
123  if (filename.isEmpty())
124  continue;
125 
126  const bool optional = item->checkState() == Qt::Unchecked;
127  demo.wads << PWad(filename, optional);
128  }
129  return demo;
130 }
131 
132 void DemoMetaDataDialog::removeSelectedWads()
133 {
134  const bool bSelectNextLowest = true;
135  CommonGUI::removeSelectedRowsFromStandardItemView(d->wadsList, bSelectNextLowest);
136 }