2011-04-07 22:04:09 +00:00
|
|
|
#include <QtGui>
|
|
|
|
|
2012-09-23 18:41:41 +00:00
|
|
|
#include <components/esm/esmreader.hpp>
|
2012-01-21 00:14:35 +00:00
|
|
|
#include <components/files/configurationmanager.hpp>
|
2011-04-07 22:04:09 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
#include "model/datafilesmodel.hpp"
|
|
|
|
#include "model/esm/esmfile.hpp"
|
|
|
|
|
2012-10-12 00:25:14 +00:00
|
|
|
#include "utils/lineedit.hpp"
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
#include "datafileslist.hpp"
|
2011-04-07 22:04:09 +00:00
|
|
|
|
2012-03-12 13:42:24 +00:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
/**
|
|
|
|
* Workaround for problems with whitespaces in paths in older versions of Boost library
|
|
|
|
*/
|
|
|
|
#if (BOOST_VERSION <= 104600)
|
|
|
|
namespace boost
|
|
|
|
{
|
|
|
|
|
|
|
|
template<>
|
|
|
|
inline boost::filesystem::path lexical_cast<boost::filesystem::path, std::string>(const std::string& arg)
|
|
|
|
{
|
|
|
|
return boost::filesystem::path(arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace boost */
|
|
|
|
#endif /* (BOOST_VERSION <= 104600) */
|
|
|
|
|
2011-04-07 22:04:09 +00:00
|
|
|
using namespace ESM;
|
2011-05-11 20:23:37 +00:00
|
|
|
using namespace std;
|
2011-04-07 22:04:09 +00:00
|
|
|
|
2011-06-09 22:30:08 +00:00
|
|
|
//sort QModelIndexList ascending
|
|
|
|
bool rowGreaterThan(const QModelIndex &index1, const QModelIndex &index2)
|
|
|
|
{
|
|
|
|
return index1.row() >= index2.row();
|
|
|
|
}
|
|
|
|
|
|
|
|
//sort QModelIndexList descending
|
|
|
|
bool rowSmallerThan(const QModelIndex &index1, const QModelIndex &index2)
|
|
|
|
{
|
|
|
|
return index1.row() <= index2.row();
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
DataFilesList::DataFilesList(Files::ConfigurationManager &cfg, QWidget *parent)
|
2012-01-21 00:14:35 +00:00
|
|
|
: QWidget(parent)
|
|
|
|
, mCfgMgr(cfg)
|
2011-04-07 22:04:09 +00:00
|
|
|
{
|
2013-02-10 13:02:06 +00:00
|
|
|
// Model
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel = new DataFilesModel(this);
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesProxyModel = new QSortFilterProxyModel();
|
|
|
|
mFilesProxyModel->setDynamicSortFilter(true);
|
|
|
|
mFilesProxyModel->setSourceModel(mFilesModel);
|
2011-05-11 18:04:25 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
// Filter toolbar
|
2011-06-24 15:38:32 +00:00
|
|
|
QLabel *filterLabel = new QLabel(tr("&Filter:"), this);
|
2011-04-07 22:04:09 +00:00
|
|
|
LineEdit *filterLineEdit = new LineEdit(this);
|
2011-06-24 15:38:32 +00:00
|
|
|
filterLabel->setBuddy(filterLineEdit);
|
|
|
|
|
|
|
|
QToolBar *filterToolBar = new QToolBar(this);
|
|
|
|
filterToolBar->setMovable(false);
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2011-06-24 15:38:32 +00:00
|
|
|
// Create a container widget and a layout to get the spacer to work
|
|
|
|
QWidget *filterWidget = new QWidget(this);
|
|
|
|
QHBoxLayout *filterLayout = new QHBoxLayout(filterWidget);
|
2011-04-07 22:04:09 +00:00
|
|
|
QSpacerItem *hSpacer1 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2011-06-24 15:38:32 +00:00
|
|
|
filterLayout->addItem(hSpacer1);
|
|
|
|
filterLayout->addWidget(filterLabel);
|
|
|
|
filterLayout->addWidget(filterLineEdit);
|
|
|
|
|
|
|
|
filterToolBar->addWidget(filterWidget);
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
QCheckBox checkBox;
|
|
|
|
unsigned int height = checkBox.sizeHint().height() + 4;
|
2011-06-08 14:55:53 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesTable = new QTableView(this);
|
|
|
|
mFilesTable->setModel(mFilesProxyModel);
|
|
|
|
mFilesTable->setObjectName("PluginsTable");
|
|
|
|
mFilesTable->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
mFilesTable->setSelectionBehavior(QAbstractItemView::SelectRows);
|
|
|
|
mFilesTable->setSelectionMode(QAbstractItemView::SingleSelection);
|
|
|
|
mFilesTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
|
|
|
|
mFilesTable->setAlternatingRowColors(true);
|
|
|
|
mFilesTable->setVerticalScrollMode(QAbstractItemView::ScrollPerItem);
|
|
|
|
mFilesTable->horizontalHeader()->setStretchLastSection(true);
|
|
|
|
mFilesTable->horizontalHeader()->hide();
|
|
|
|
|
|
|
|
mFilesTable->verticalHeader()->setDefaultSectionSize(height);
|
|
|
|
mFilesTable->verticalHeader()->setResizeMode(QHeaderView::Fixed);
|
|
|
|
mFilesTable->setColumnHidden(1, true);
|
|
|
|
mFilesTable->setColumnHidden(2, true);
|
|
|
|
mFilesTable->setColumnHidden(3, true);
|
|
|
|
mFilesTable->setColumnHidden(4, true);
|
|
|
|
mFilesTable->setColumnHidden(5, true);
|
|
|
|
mFilesTable->setColumnHidden(6, true);
|
|
|
|
mFilesTable->setColumnHidden(7, true);
|
|
|
|
mFilesTable->setColumnHidden(8, true);
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2011-04-07 22:04:09 +00:00
|
|
|
QVBoxLayout *pageLayout = new QVBoxLayout(this);
|
2011-06-07 18:21:01 +00:00
|
|
|
|
2011-06-24 15:38:32 +00:00
|
|
|
pageLayout->addWidget(filterToolBar);
|
2013-02-10 13:08:54 +00:00
|
|
|
pageLayout->addWidget(mFilesTable);
|
2012-10-22 23:47:07 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
connect(mFilesTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckState(QModelIndex)));
|
2013-02-10 13:02:06 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
connect(mFilesModel, SIGNAL(checkedItemsChanged(QStringList,QStringList)), mFilesModel, SLOT(slotcheckedItemsChanged(QStringList,QStringList)));
|
2011-04-24 21:29:32 +00:00
|
|
|
|
2012-10-22 23:47:07 +00:00
|
|
|
connect(filterLineEdit, SIGNAL(textChanged(QString)), this, SLOT(filterChanged(QString)));
|
2011-05-11 18:04:25 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
connect(mFilesTable, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showContextMenu(QPoint)));
|
2011-06-07 18:21:01 +00:00
|
|
|
|
2012-02-28 16:19:44 +00:00
|
|
|
createActions();
|
2012-10-10 20:58:04 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
void DataFilesList::createActions()
|
2012-10-10 20:58:04 +00:00
|
|
|
{
|
|
|
|
// Refresh the plugins
|
|
|
|
QAction *refreshAction = new QAction(tr("Refresh"), this);
|
|
|
|
refreshAction->setShortcut(QKeySequence(tr("F5")));
|
|
|
|
connect(refreshAction, SIGNAL(triggered()), this, SLOT(refresh()));
|
|
|
|
|
|
|
|
// Context menu actions
|
|
|
|
mCheckAction = new QAction(tr("Check selected"), this);
|
|
|
|
connect(mCheckAction, SIGNAL(triggered()), this, SLOT(check()));
|
|
|
|
|
|
|
|
mUncheckAction = new QAction(tr("Uncheck selected"), this);
|
|
|
|
connect(mUncheckAction, SIGNAL(triggered()), this, SLOT(uncheck()));
|
|
|
|
|
|
|
|
// Context menu for the plugins table
|
|
|
|
mContextMenu = new QMenu(this);
|
|
|
|
|
|
|
|
mContextMenu->addAction(mCheckAction);
|
|
|
|
mContextMenu->addAction(mUncheckAction);
|
2012-02-28 16:19:44 +00:00
|
|
|
|
2011-04-07 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 21:14:14 +00:00
|
|
|
bool DataFilesList::setupDataFiles(Files::PathContainer dataDirs, const QString encoding)
|
2011-05-02 20:21:42 +00:00
|
|
|
{
|
2012-10-30 10:26:48 +00:00
|
|
|
// Set the charset for reading the esm/esp files
|
|
|
|
if (!encoding.isEmpty() && encoding != QLatin1String("win1252")) {
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->setEncoding(encoding);
|
2012-10-30 10:26:48 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
// Add the paths to the respective models
|
2013-02-04 21:14:14 +00:00
|
|
|
for (Files::PathContainer::iterator it = dataDirs.begin(); it != dataDirs.end(); ++it) {
|
2012-10-10 20:58:04 +00:00
|
|
|
QString path = QString::fromStdString(it->string());
|
|
|
|
path.remove(QChar('\"'));
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->addFiles(path);
|
2012-10-10 20:58:04 +00:00
|
|
|
}
|
2012-09-16 02:28:51 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->sort(0);
|
2012-10-30 05:47:39 +00:00
|
|
|
// mMastersTable->sortByColumn(3, Qt::AscendingOrder);
|
|
|
|
// mPluginsTable->sortByColumn(3, Qt::AscendingOrder);
|
2012-09-16 02:28:51 +00:00
|
|
|
|
2012-06-20 18:43:51 +00:00
|
|
|
return true;
|
2011-04-07 22:04:09 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 21:14:14 +00:00
|
|
|
void DataFilesList::selectedFiles(std::vector<boost::filesystem::path>& paths)
|
2013-01-31 23:42:03 +00:00
|
|
|
{
|
2013-02-10 13:08:54 +00:00
|
|
|
QStringList pluginPaths = mFilesModel->checkedItemsPaths();
|
2013-01-31 23:42:03 +00:00
|
|
|
foreach (const QString &path, pluginPaths)
|
|
|
|
{
|
|
|
|
paths.push_back(path.toStdString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
void DataFilesList::check()
|
2011-06-07 18:21:01 +00:00
|
|
|
{
|
|
|
|
// Check the current selection
|
2013-02-10 13:08:54 +00:00
|
|
|
if (!mFilesTable->selectionModel()->hasSelection()) {
|
2011-06-07 18:21:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
QModelIndexList indexes = mFilesTable->selectionModel()->selectedIndexes();
|
2011-06-07 18:21:01 +00:00
|
|
|
|
2011-06-09 22:30:08 +00:00
|
|
|
//sort selection ascending because selectedIndexes returns an unsorted list
|
2012-10-10 20:58:04 +00:00
|
|
|
//qSort(indexes.begin(), indexes.end(), rowSmallerThan);
|
2011-06-09 22:30:08 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
foreach (const QModelIndex &index, indexes) {
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2011-06-07 18:21:01 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->setCheckState(index, Qt::Checked);
|
2011-06-07 18:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
void DataFilesList::uncheck()
|
2011-06-07 18:21:01 +00:00
|
|
|
{
|
2012-10-10 20:58:04 +00:00
|
|
|
// uncheck the current selection
|
2013-02-10 13:08:54 +00:00
|
|
|
if (!mFilesTable->selectionModel()->hasSelection()) {
|
2011-06-07 18:21:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
QModelIndexList indexes = mFilesTable->selectionModel()->selectedIndexes();
|
2011-06-07 18:21:01 +00:00
|
|
|
|
2011-06-09 22:30:08 +00:00
|
|
|
//sort selection ascending because selectedIndexes returns an unsorted list
|
2012-10-10 20:58:04 +00:00
|
|
|
//qSort(indexes.begin(), indexes.end(), rowSmallerThan);
|
2011-06-09 22:30:08 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
foreach (const QModelIndex &index, indexes) {
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2011-06-07 18:21:01 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->setCheckState(index, Qt::Unchecked);
|
2011-06-07 18:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
void DataFilesList::refresh()
|
2011-06-07 19:14:27 +00:00
|
|
|
{
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->sort(0);
|
2012-10-10 20:58:04 +00:00
|
|
|
|
2011-06-07 19:14:27 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
// Refresh the plugins table
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesTable->scrollToTop();
|
2011-06-07 19:14:27 +00:00
|
|
|
}
|
|
|
|
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
void DataFilesList::setCheckState(QModelIndex index)
|
2011-04-07 22:04:09 +00:00
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
QObject *object = QObject::sender();
|
2011-04-07 22:04:09 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
// Not a signal-slot call
|
|
|
|
if (!object)
|
2011-04-07 22:04:09 +00:00
|
|
|
return;
|
2011-04-24 19:42:56 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
if (object->objectName() == QLatin1String("PluginsTable")) {
|
2013-02-10 13:08:54 +00:00
|
|
|
QModelIndex sourceIndex = mFilesProxyModel->mapToSource(index);
|
2012-10-22 23:47:07 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
(mFilesModel->checkState(sourceIndex) == Qt::Checked)
|
|
|
|
? mFilesModel->setCheckState(sourceIndex, Qt::Unchecked)
|
|
|
|
: mFilesModel->setCheckState(sourceIndex, Qt::Checked);
|
2011-04-24 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
return;
|
2011-04-24 21:23:04 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-02-04 21:14:14 +00:00
|
|
|
void DataFilesList::uncheckAll()
|
2011-05-11 18:04:25 +00:00
|
|
|
{
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->uncheckAll();
|
2011-04-24 21:29:32 +00:00
|
|
|
}
|
2011-04-24 21:03:21 +00:00
|
|
|
|
2013-02-04 21:14:14 +00:00
|
|
|
void DataFilesList::filterChanged(const QString filter)
|
2012-10-22 23:47:07 +00:00
|
|
|
{
|
2013-02-04 21:14:14 +00:00
|
|
|
QRegExp regExp(filter, Qt::CaseInsensitive, QRegExp::FixedString);
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesProxyModel->setFilterRegExp(regExp);
|
2012-10-22 23:47:07 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 20:08:27 +00:00
|
|
|
void DataFilesList::showContextMenu(const QPoint &point)
|
2011-04-24 21:03:21 +00:00
|
|
|
{
|
2012-10-10 20:58:04 +00:00
|
|
|
// Make sure there are plugins in the view
|
2013-02-10 13:08:54 +00:00
|
|
|
if (!mFilesTable->selectionModel()->hasSelection()) {
|
2012-02-28 16:19:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
QPoint globalPos = mFilesTable->mapToGlobal(point);
|
2011-04-25 08:41:16 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
QModelIndexList indexes = mFilesTable->selectionModel()->selectedIndexes();
|
2011-04-25 08:41:16 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
// Show the check/uncheck actions depending on the state of the selected items
|
|
|
|
mUncheckAction->setEnabled(false);
|
|
|
|
mCheckAction->setEnabled(false);
|
2011-04-24 21:03:21 +00:00
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
foreach (const QModelIndex &index, indexes) {
|
|
|
|
if (!index.isValid())
|
|
|
|
return;
|
2011-04-24 21:15:55 +00:00
|
|
|
|
2013-02-10 13:08:54 +00:00
|
|
|
(mFilesModel->checkState(index) == Qt::Checked)
|
2012-10-22 23:47:07 +00:00
|
|
|
? mUncheckAction->setEnabled(true)
|
|
|
|
: mCheckAction->setEnabled(true);
|
2011-04-24 21:03:21 +00:00
|
|
|
}
|
|
|
|
|
2012-10-10 20:58:04 +00:00
|
|
|
// Show menu
|
|
|
|
mContextMenu->exec(globalPos);
|
2011-04-24 21:03:21 +00:00
|
|
|
}
|
2013-02-04 21:14:14 +00:00
|
|
|
|
|
|
|
void DataFilesList::setCheckState(const QString& element, Qt::CheckState state)
|
|
|
|
{
|
2013-02-10 13:08:54 +00:00
|
|
|
EsmFile *file = mFilesModel->findItem(element);
|
2013-02-04 21:14:14 +00:00
|
|
|
if (file)
|
|
|
|
{
|
2013-02-10 13:08:54 +00:00
|
|
|
mFilesModel->setCheckState(mFilesModel->indexFromItem(file), Qt::Checked);
|
2013-02-04 21:14:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList DataFilesList::checkedFiles()
|
|
|
|
{
|
2013-02-10 13:08:54 +00:00
|
|
|
return mFilesModel->checkedItems();
|
2013-02-15 14:21:14 +00:00
|
|
|
}
|