-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathhyp_commonfunc.cpp
194 lines (154 loc) · 5.74 KB
/
hyp_commonfunc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* This file (hyp_commonfunc.cpp) is part of Rocket Launcher 2.0 - A cross platform
* front end for all DOOM engine source ports.
*
* Copyright (C) Hypnotoad
*
* Rocket Launcher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rocket Launcher is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rocket Launcher. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hyp_commonfunc.h"
#include <QString>
#include <QFileInfo>
#include <QStandardItemModel>
#include <QDebug>
#include "dndfilesystemlistview.h"
bool inputExists (QString &name)
{
return !(name == "" || name.isEmpty() || name.isNull());
}
bool fileExists (QString &file)
{
QFileInfo fi(file);
return (fi.exists());
}
bool isFile(QString &file)
{
QFileInfo fi(file);
return (fi.isFile());
}
void removeSelectedFromDnDListView(DndFileSystemListView *listview, QStandardItemModel *model)
{
listview->setUpdatesEnabled(false);
QModelIndexList indexes = listview->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end());
for (int i = indexes.count() - 1; i > -1; --i)
{
model->removeRow(indexes.at(i).row());
}
listview->setUpdatesEnabled(true);
}
void removeSelectedFromDnDListViewSave(DndFileSystemListView *listview, QStandardItemModel *model, QString ArrayName, QString key, QSettings &settings)
{
listview->setUpdatesEnabled(false);
QModelIndexList indexes = listview->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end());
for (int i = indexes.count() - 1; i > -1; --i)
{
model->removeRow(indexes.at(i).row());
}
settings.beginWriteArray(ArrayName);
settings.remove("");
for (int i = 0; i < model->rowCount(); i++)
{
settings.setArrayIndex(i);
settings.setValue(key, model->item(i)->data(Qt::UserRole).toString());
}
settings.endArray();
listview->setUpdatesEnabled(true);
}
bool updateDndListView(QString filepath, QStandardItemModel *model, bool checkable, bool checksate)
{
QFileInfo file(filepath);
if (!file.exists())
return false;
QStandardItem *item = new QStandardItem();
item->setData(file.fileName(),Qt::DisplayRole);
item->setData(file.absoluteFilePath(),Qt::UserRole);
item->setDragEnabled(true);
item->setDropEnabled(false);
if (checkable)
{
item->setCheckable(true);
if (checksate)
item->setCheckState(Qt::Checked);
else
item->setCheckState(Qt::Unchecked);
}
if (model->match(model->index(0,0) , Qt::UserRole, file.absoluteFilePath()).size() > 0)
return false;
model->appendRow(item);
return true;
}
void saveListviewPath(QString ArrayName, QString key, QString Path, QSettings &settings)
{
int fsize = settings.beginReadArray(ArrayName);
settings.endArray();
settings.beginWriteArray(ArrayName);
settings.setArrayIndex(fsize);
settings.setValue(key, Path);
settings.endArray();
}
void copyItemToDndListView(DndFileSystemListView *source, QStandardItemModel *DestinationModel, bool checkable)
{
QModelIndexList indexes = source->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end());
for (int i = indexes.count() - 1; i > -1; --i)
updateDndListView(indexes.at(i).data(Qt::UserRole).toString(), DestinationModel, checkable);
}
void copyItemToDndListViewSave(DndFileSystemListView *source, QStandardItemModel *DestinationModel, bool checkable, QString array, QString key, QSettings &settings)
{
QModelIndexList indexes = source->selectionModel()->selectedIndexes();
qSort(indexes.begin(), indexes.end());
for (int i = indexes.count() - 1; i > -1; --i)
{
if (updateDndListView(indexes.at(i).data(Qt::UserRole).toString(), DestinationModel, checkable))
saveListviewPath(array, key, indexes.at(i).data(Qt::UserRole).toString(), settings);
}
}
QString returnSelectedDndViewItemData(DndFileSystemListView *listview, int role)
{
QModelIndexList indexes = listview->selectionModel()->selectedIndexes();
if (indexes.count() < 1)
return "";
qSort(indexes.begin(), indexes.end());
return indexes.at(0).data(role).toString();
}
QModelIndex getIndexOfDisplayText(QAbstractItemModel *model, QString text)
{
QModelIndexList indexes = model->match(model->index(0,0), Qt::DisplayRole,QVariant::fromValue(text));
if (indexes.count()> 0)
return indexes.at(0);
else
return QModelIndex();
}
QStringList splitArgs(QString string) //useful approach, found here: qtcentre.org/threads/37304-Split-strings-using-QStringList-split%28%29-but-ignore-quotes?p=213884#post213884
{
bool inside = false;
if (string.at(0) == '\"')
inside = true; //true if the first character is "
QStringList tmpList = string.split(QRegExp("\""), QString::SkipEmptyParts); // Split by " and make sure you don't have an empty string at the beginning
QStringList retlist;
foreach (QString s, tmpList)
{
if (inside)
{ // If 's' is inside quotes ...
retlist.append(s.trimmed()); // ... get the whole string
}
else
{ // If 's' is outside quotes ...
retlist.append(s.split(" ", QString::SkipEmptyParts)); // ... get the splitted string
}
inside = !inside;
}
return retlist;
}