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