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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
#include "contentselector.hpp"
#include <components/contentselector/model/esmfile.hpp>
#include <QSortFilterProxyModel>
#include <QMenu>
#include <QContextMenuEvent>
#include <QClipboard>
#include <QModelIndex>
ContentSelectorView::ContentSelector::ContentSelector(QWidget *parent) :
QObject(parent)
{
ui.setupUi(parent);
ui.addonView->setDragDropMode(QAbstractItemView::InternalMove);
buildContentModel();
buildGameFileView();
buildAddonView();
}
void ContentSelectorView::ContentSelector::buildContentModel()
{
QIcon warningIcon(ui.addonView->style()->standardIcon(QStyle::SP_MessageBoxWarning).pixmap(QSize(16, 15)));
mContentModel = new ContentSelectorModel::ContentModel(this, warningIcon);
}
void ContentSelectorView::ContentSelector::buildGameFileView()
{
ui.gameFileView->setVisible (true);
ui.gameFileView->setPlaceholderText(QString("Select a game file..."));
connect (ui.gameFileView, SIGNAL (currentIndexChanged(int)),
this, SLOT (slotCurrentGameFileIndexChanged(int)));
ui.gameFileView->setCurrentIndex(-1);
ui.gameFileView->setCurrentIndex(0);
}
class AddOnProxyModel : public QSortFilterProxyModel
{
public:
explicit AddOnProxyModel(QObject* parent = nullptr) :
QSortFilterProxyModel(parent)
{}
bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override
{
static const QString ContentTypeAddon = QString::number((int)ContentSelectorModel::ContentType_Addon);
QModelIndex nameIndex = sourceModel()->index(sourceRow, 0, sourceParent);
const QString userRole = sourceModel()->data(nameIndex, Qt::UserRole).toString();
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent) && userRole == ContentTypeAddon;
}
};
void ContentSelectorView::ContentSelector::buildAddonView()
{
ui.addonView->setVisible (true);
mAddonProxyModel = new AddOnProxyModel(this);
mAddonProxyModel->setFilterRegExp(searchFilter()->text());
mAddonProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
mAddonProxyModel->setDynamicSortFilter (true);
mAddonProxyModel->setSourceModel (mContentModel);
connect(ui.searchFilter, SIGNAL(textEdited(QString)), mAddonProxyModel, SLOT(setFilterWildcard(QString)));
connect(ui.searchFilter, SIGNAL(textEdited(QString)), this, SLOT(slotSearchFilterTextChanged(QString)));
ui.addonView->setModel(mAddonProxyModel);
connect(ui.addonView, SIGNAL(activated(const QModelIndex&)), this, SLOT(slotAddonTableItemActivated(const QModelIndex&)));
connect(mContentModel, SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SIGNAL(signalAddonDataChanged(QModelIndex,QModelIndex)));
buildContextMenu();
}
void ContentSelectorView::ContentSelector::buildContextMenu()
{
ui.addonView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui.addonView, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(slotShowContextMenu(const QPoint&)));
mContextMenu = new QMenu(ui.addonView);
mContextMenu->addAction(tr("&Check Selected"), this, SLOT(slotCheckMultiSelectedItems()));
mContextMenu->addAction(tr("&Uncheck Selected"), this, SLOT(slotUncheckMultiSelectedItems()));
mContextMenu->addAction(tr("&Copy Path(s) to Clipboard"), this, SLOT(slotCopySelectedItemsPaths()));
}
void ContentSelectorView::ContentSelector::setProfileContent(const QStringList &fileList)
{
clearCheckStates();
for (const QString &filepath : fileList)
{
const ContentSelectorModel::EsmFile *file = mContentModel->item(filepath);
if (file && file->isGameFile())
{
setGameFile (filepath);
break;
}
}
setContentList(fileList);
}
void ContentSelectorView::ContentSelector::setGameFile(const QString &filename)
{
int index = -1;
if (!filename.isEmpty())
{
const ContentSelectorModel::EsmFile *file = mContentModel->item (filename);
index = ui.gameFileView->findText (file->fileName());
//verify that the current index is also checked in the model
if (!mContentModel->setCheckState(filename, true))
{
//throw error in case file not found?
return;
}
}
ui.gameFileView->setCurrentIndex(index);
}
void ContentSelectorView::ContentSelector::clearCheckStates()
{
mContentModel->uncheckAll();
}
void ContentSelectorView::ContentSelector::setEncoding(const QString &encoding)
{
mContentModel->setEncoding(encoding);
}
void ContentSelectorView::ContentSelector::setContentList(const QStringList &list)
{
if (list.isEmpty())
{
slotCurrentGameFileIndexChanged (ui.gameFileView->currentIndex());
}
else
mContentModel->setContentList(list);
}
ContentSelectorModel::ContentFileList
ContentSelectorView::ContentSelector::selectedFiles() const
{
if (!mContentModel)
return ContentSelectorModel::ContentFileList();
return mContentModel->checkedItems();
}
void ContentSelectorView::ContentSelector::addFiles(const QString &path)
{
mContentModel->addFiles(path);
// add any game files to the combo box
for (const QString& gameFileName : mContentModel->gameFiles())
{
if (ui.gameFileView->findText(gameFileName) == -1)
{
ui.gameFileView->addItem(gameFileName);
}
}
if (ui.gameFileView->currentIndex() != -1)
ui.gameFileView->setCurrentIndex(-1);
mContentModel->uncheckAll();
}
void ContentSelectorView::ContentSelector::clearFiles()
{
mContentModel->clearFiles();
}
QString ContentSelectorView::ContentSelector::currentFile() const
{
QModelIndex currentIdx = ui.addonView->currentIndex();
if (!currentIdx.isValid())
return ui.gameFileView->currentText();
QModelIndex idx = mContentModel->index(mAddonProxyModel->mapToSource(currentIdx).row(), 0, QModelIndex());
return mContentModel->data(idx, Qt::DisplayRole).toString();
}
void ContentSelectorView::ContentSelector::slotCurrentGameFileIndexChanged(int index)
{
static int oldIndex = -1;
if (index != oldIndex)
{
if (oldIndex > -1)
{
setGameFileSelected(oldIndex, false);
}
oldIndex = index;
setGameFileSelected(index, true);
mContentModel->checkForLoadOrderErrors();
}
emit signalCurrentGamefileIndexChanged (index);
}
void ContentSelectorView::ContentSelector::setGameFileSelected(int index, bool selected)
{
QString fileName = ui.gameFileView->itemText(index);
const ContentSelectorModel::EsmFile* file = mContentModel->item(fileName);
if (file != nullptr)
{
QModelIndex index2(mContentModel->indexFromItem(file));
mContentModel->setData(index2, selected, Qt::UserRole + 1);
}
}
void ContentSelectorView::ContentSelector::slotAddonTableItemActivated(const QModelIndex &index)
{
// toggles check state when an AddOn file is double clicked or activated by keyboard
QModelIndex sourceIndex = mAddonProxyModel->mapToSource (index);
if (!mContentModel->isEnabled (sourceIndex))
return;
Qt::CheckState checkState = Qt::Unchecked;
if (mContentModel->data(sourceIndex, Qt::CheckStateRole).toInt() == Qt::Unchecked)
checkState = Qt::Checked;
mContentModel->setData(sourceIndex, checkState, Qt::CheckStateRole);
}
void ContentSelectorView::ContentSelector::slotShowContextMenu(const QPoint& pos)
{
QPoint globalPos = ui.addonView->viewport()->mapToGlobal(pos);
mContextMenu->exec(globalPos);
}
void ContentSelectorView::ContentSelector::setCheckStateForMultiSelectedItems(bool checked)
{
Qt::CheckState checkState = checked ? Qt::Checked : Qt::Unchecked;
for (const QModelIndex& index : ui.addonView->selectionModel()->selectedIndexes())
{
QModelIndex sourceIndex = mAddonProxyModel->mapToSource(index);
if (mContentModel->data(sourceIndex, Qt::CheckStateRole).toInt() != checkState)
{
mContentModel->setData(sourceIndex, checkState, Qt::CheckStateRole);
}
}
}
void ContentSelectorView::ContentSelector::slotUncheckMultiSelectedItems()
{
setCheckStateForMultiSelectedItems(false);
}
void ContentSelectorView::ContentSelector::slotCheckMultiSelectedItems()
{
setCheckStateForMultiSelectedItems(true);
}
void ContentSelectorView::ContentSelector::slotCopySelectedItemsPaths()
{
QClipboard *clipboard = QApplication::clipboard();
QString filepaths;
for (const QModelIndex& index : ui.addonView->selectionModel()->selectedIndexes())
{
int row = mAddonProxyModel->mapToSource(index).row();
const ContentSelectorModel::EsmFile *file = mContentModel->item(row);
filepaths += file->filePath() + "\n";
}
if (!filepaths.isEmpty())
{
clipboard->setText(filepaths);
}
}
void ContentSelectorView::ContentSelector::slotSearchFilterTextChanged(const QString& newText)
{
ui.addonView->setDragEnabled(newText.isEmpty());
}
|