cfgwadseekersites.cpp
1 //------------------------------------------------------------------------------
2 // cfgwadseekersites.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) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "cfgwadseekersites.h"
24 #include "configuration/doomseekerconfig.h"
25 #include "ui_cfgwadseekersites.h"
26 #include "wadseeker/wadseeker.h"
27 #include <QCompleter>
28 #include <QDebug>
29 #include <QFileDialog>
30 #include <QMessageBox>
31 #include <QStandardItemModel>
32 #include <QUrl>
33 
34 DClass<CFGWadseekerSites> : public Ui::CFGWadseekerSites
35 {
36 };
37 
38 DPointered(CFGWadseekerSites)
39 
40 CFGWadseekerSites::CFGWadseekerSites(QWidget *parent)
41  : ConfigPage(parent)
42 {
43  d->setupUi(this);
44 
45  d->lstUrls->setModel(new QStandardItemModel());
46 
47  connect(d->btnUrlAdd, SIGNAL(clicked()), this, SLOT(btnUrlAddClicked()));
48  connect(d->btnUrlDefault, SIGNAL(clicked()), this, SLOT(btnUrlDefaultClicked()));
49  connect(d->btnUrlRemove, SIGNAL(clicked()), this, SLOT(btnUrlRemoveClicked()));
50  connect(d->leUrl, SIGNAL(returnPressed()), this, SLOT(btnUrlAddClicked()));
51 }
52 
53 CFGWadseekerSites::~CFGWadseekerSites()
54 {
55 }
56 
57 void CFGWadseekerSites::btnUrlAddClicked()
58 {
59  insertUrl(d->leUrl->text());
60 }
61 
62 void CFGWadseekerSites::btnUrlDefaultClicked()
63 {
64  for (int i = 0; !Wadseeker::defaultSites[i].isEmpty(); ++i)
65  insertUrl(Wadseeker::defaultSites[i]);
66 }
67 
68 void CFGWadseekerSites::btnUrlRemoveClicked()
69 {
70  QItemSelectionModel *selModel = d->lstUrls->selectionModel();
71  QModelIndexList indexList = selModel->selectedIndexes();
72  selModel->clear();
73 
74  auto model = static_cast<QStandardItemModel *>(d->lstUrls->model());
75  QList<QStandardItem *> itemList;
76  for (int i = 0; i < indexList.count(); ++i)
77  itemList << model->itemFromIndex(indexList[i]);
78 
79  for (int i = 0; i < itemList.count(); ++i)
80  {
81  QModelIndex index = model->indexFromItem(itemList[i]);
82  model->removeRow(index.row());
83  }
84 }
85 
86 void CFGWadseekerSites::insertUrl(const QString &url)
87 {
88  if (url.isEmpty())
89  return;
90 
91  // first we check whether the URL is already in the box.
92  auto model = static_cast<QStandardItemModel *>(d->lstUrls->model());
93  for (int i = 0; i < model->rowCount(); ++i)
94  {
95  QUrl existingUrl(model->item(i)->text());
96  if (existingUrl == url)
97  return;
98  }
99 
100  auto it = new QStandardItem(url);
101 
102  it->setDragEnabled(true);
103  it->setDropEnabled(false);
104  it->setToolTip(url);
105 
106  model->appendRow(it);
107 }
108 
110 {
111  const QStringList &urlList = gConfig.wadseeker.searchURLs;
112  for (const QString &url : urlList)
113  {
114  this->insertUrl(url);
115  }
116  d->cbAlwaysUseDefaultSites->setChecked(gConfig.wadseeker.bAlwaysUseDefaultSites);
117 }
118 
120 {
121  QStringList urlList;
122  auto model = static_cast<QStandardItemModel *>(d->lstUrls->model());
123  for (int i = 0; i < model->rowCount(); ++i)
124  urlList << model->item(i)->text();
125 
126  gConfig.wadseeker.searchURLs = urlList;
127  gConfig.wadseeker.bAlwaysUseDefaultSites = d->cbAlwaysUseDefaultSites->isChecked();
128 }