From 74eef829020408d0954b9eb4cd9cbdd244425fb9 Mon Sep 17 00:00:00 2001 From: Pieter van der Kloet Date: Tue, 29 Mar 2011 23:42:34 +0200 Subject: [PATCH] Messing around with a new datafilesdialog --- apps/launcher/CMakeLists.txt | 4 + apps/launcher/datafilesdialog.cpp | 123 ++++++---------- apps/launcher/datafilesdialog.h | 29 ++-- apps/launcher/datafilesmodel.cpp | 230 ++++++++++++++++-------------- apps/launcher/datafilesmodel.h | 49 +++---- apps/launcher/launcher.pro | 6 +- 6 files changed, 212 insertions(+), 229 deletions(-) diff --git a/apps/launcher/CMakeLists.txt b/apps/launcher/CMakeLists.txt index 291103b28c..fe29375d96 100644 --- a/apps/launcher/CMakeLists.txt +++ b/apps/launcher/CMakeLists.txt @@ -1,12 +1,14 @@ set(LAUNCHER datafilesdialog.cpp datafilesmodel.cpp + datafilesitem.cpp lineedit.cpp main.cpp maindialog.cpp settingsdialog.cpp datafilesdialog.h datafilesmodel.h + datafilesitem.h lineedit.h maindialog.h settingsdialog.h @@ -14,6 +16,8 @@ set(LAUNCHER SET(MOC_HDRS datafilesdialog.h + datafilesmodel.h + datafilesitem.h lineedit.h maindialog.h settingsdialog.h diff --git a/apps/launcher/datafilesdialog.cpp b/apps/launcher/datafilesdialog.cpp index bab3f893ca..b906cb75b5 100644 --- a/apps/launcher/datafilesdialog.cpp +++ b/apps/launcher/datafilesdialog.cpp @@ -3,27 +3,34 @@ #include "datafilesdialog.h" #include "datafilesmodel.h" +#include "datafilesitem.h" using namespace ESM; -//DataFilesDialog::DataFilesDialog(QWidget *parent) -// : QDialog(parent) - DataFilesDialog::DataFilesDialog() { - //QWidget *centralWidget = new QWidget; - + //dataFilesModel = new DataFilesModel(this); dataFilesModel = new DataFilesModel(); - dataFilesModel->setReadOnly(true); // Prevent changes to files - dataFilesModel->setRootPath("data"); + + for (int row = 0; row < 4; ++row) { + for (int column = 0; column < 4; ++column) { + QList test; + test << QString("%0").arg(i); + DataFilesItem *item = new DataFilesItem(test); + dataFilesModel->setItem(row, column, item); + } + } + + //dataFilesModel->setReadOnly(true); // Prevent changes to files + //dataFilesModel->setRootPath("data"); // sortModel = new QSortFilterProxyModel(); // sortModel->setSourceModel(dataFilesModel); - selectionModel = new QItemSelectionModel(dataFilesModel); + //selectionModel = new QItemSelectionModel(dataFilesModel); // selectionModel = new QItemSelectionModel(sortModel); - // First, show only plugin files and sort them + /* First, show only plugin files and sort them QStringList acceptedfiles = (QStringList() << "*.esp"); dataFilesModel->setNameFilters(acceptedfiles); dataFilesModel->setNameFilterDisables(false); // Hide all other files @@ -35,58 +42,33 @@ DataFilesDialog::DataFilesDialog() acceptedfiles = (QStringList() << "*.esm" << "*.esp"); dataFilesModel->setNameFilters(acceptedfiles); dataFilesModel->setFilter(QDir::Files); - +*/ // Column 1 QVBoxLayout *dialogLayout = new QVBoxLayout(this); - QHBoxLayout *groupsLayout = new QHBoxLayout(); QGroupBox *groupFiles = new QGroupBox(tr("Morrowind Files"), this); groupFiles->setMinimumWidth(450); QVBoxLayout *groupFilesLayout = new QVBoxLayout(groupFiles); - QSpacerItem *vSpacer1 = new QSpacerItem(20, 2, QSizePolicy::Minimum, QSizePolicy::Fixed); + //QSpacerItem *vSpacer1 = new QSpacerItem(20, 2, QSizePolicy::Minimum, QSizePolicy::Fixed); - QHBoxLayout *filterLayout = new QHBoxLayout(); + /*QHBoxLayout *filterLayout = new QHBoxLayout(); QLabel *labelFilter = new QLabel(tr("Filter:"), groupFiles); lineFilter = new LineEdit(groupFiles); filterLayout->addWidget(labelFilter); filterLayout->addWidget(lineFilter); - + */ + // View for the game files - dataFilesView = new QTableView(groupFiles); - + //dataFilesView = new QTreeView(groupFiles); + dataFilesView = new QTreeView(groupFiles); + dataFilesView->setModel(dataFilesModel); + // Put everything in the correct layouts - groupFilesLayout->addLayout(filterLayout); - groupFilesLayout->addItem(vSpacer1); + //groupFilesLayout->addLayout(filterLayout); + //groupFilesLayout->addItem(vSpacer1); groupFilesLayout->addWidget(dataFilesView); - groupsLayout->addWidget(groupFiles); - - // Column 2 - QGroupBox *groupInfo = new QGroupBox(tr("File Information"), this); - groupInfo->setFixedWidth(250); - QVBoxLayout *groupInfoLayout = new QVBoxLayout(groupInfo); - - QSpacerItem *vSpacer2 = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Fixed); - - QLabel *labelAuthor = new QLabel(tr("Author:"), groupInfo); - lineAuthor = new QLineEdit(groupInfo); - lineAuthor->setReadOnly(true); - - QSpacerItem *vSpacer3 = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Fixed); - - QLabel *labelDesc = new QLabel(tr("Description:"), groupInfo); - textDesc = new QPlainTextEdit(groupInfo); - textDesc->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); - textDesc->setMinimumHeight(180); - textDesc->setReadOnly(true); - - QSpacerItem *vSpacer4 = new QSpacerItem(20, 10, QSizePolicy::Minimum, QSizePolicy::Fixed); - - QLabel *labelDepends = new QLabel(tr("Dependencies:"), groupInfo); - textDepends = new QPlainTextEdit(groupInfo); - textDepends->setFixedHeight(80); - textDepends->setReadOnly(true); QHBoxLayout *buttonsLayout = new QHBoxLayout(); QSpacerItem *horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); @@ -97,28 +79,14 @@ DataFilesDialog::DataFilesDialog() buttonsLayout->addItem(horizontalSpacer); buttonsLayout->addWidget(buttonBox); - - // Put everything in the correct layouts - groupInfoLayout->addItem(vSpacer2); - groupInfoLayout->addWidget(labelAuthor); - groupInfoLayout->addWidget(lineAuthor); - groupInfoLayout->addItem(vSpacer3); - groupInfoLayout->addWidget(labelDesc); - groupInfoLayout->addWidget(textDesc); - groupInfoLayout->addItem(vSpacer4); - groupInfoLayout->addWidget(labelDepends); - groupInfoLayout->addWidget(textDepends); - - groupsLayout->addWidget(groupInfo); - - dialogLayout->addLayout(groupsLayout); + + dialogLayout->addWidget(groupFiles); dialogLayout->addLayout(buttonsLayout); setWindowTitle(tr("Data Files")); - // Signals and slots - //connect(dataFilesModel, SIGNAL(directoryLoaded(const QString &)), this, SLOT(setupView())); + /*connect(dataFilesModel, SIGNAL(directoryLoaded(const QString &)), this, SLOT(setupView())); connect(dataFilesView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(setCheckstate(QModelIndex))); connect(selectionModel, SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(changeData(QModelIndex, QModelIndex))); @@ -128,14 +96,14 @@ DataFilesDialog::DataFilesDialog() connect(buttonBox->button(QDialogButtonBox::RestoreDefaults), SIGNAL(clicked()), this, SLOT(restoreDefaults())); connect(buttonBox, SIGNAL(accepted()), this, SLOT(writeConfig())); connect(buttonBox, SIGNAL(rejected()), this, SLOT(close())); - - readConfig(); - setupView(); +*/ + //readConfig(); + //setupView(); } void DataFilesDialog::changeData(QModelIndex index, QModelIndex bottom) { - if (!index.isValid()) { + /*if (!index.isValid()) { return; } @@ -176,7 +144,7 @@ void DataFilesDialog::changeData(QModelIndex index, QModelIndex bottom) void DataFilesDialog::setupView() { - // The signal directoryLoaded is emitted after all files are in the model + /* The signal directoryLoaded is emitted after all files are in the model dataFilesView->setModel(dataFilesModel); // dataFilesView->setModel(sortModel); @@ -203,13 +171,13 @@ void DataFilesDialog::setupView() dataFilesView->horizontalHeader()->setStretchLastSection(true); - //sortModel->setSortCaseSensitivity(Qt::CaseInsensitive); + //sortModel->setSortCaseSensitivity(Qt::CaseInsensitive);*/ } void DataFilesDialog::readConfig() { - QString filename; +/* QString filename; QString path = "data/"; // TODO: Should be global QFile file("openmw.cfg"); // Specify filepath later @@ -250,12 +218,12 @@ void DataFilesDialog::readConfig() // File is found in model, set it to checked dataFilesModel->setData(index, Qt::Checked, Qt::CheckStateRole); } - } + }*/ } void DataFilesDialog::writeConfig() { - // Custom write method: We cannot use QSettings because it does not accept spaces + /* Custom write method: We cannot use QSettings because it does not accept spaces // QList checkeditems = dataFilesModel->getCheckedItems().toList(); QStringList checkeditems = dataFilesModel->getCheckedItems(); @@ -318,12 +286,12 @@ void DataFilesDialog::writeConfig() file.close(); - close(); // Exit dialog + close(); // Exit dialog*/ } void DataFilesDialog::restoreDefaults() { - // Uncheck all checked items +/* // Uncheck all checked items dataFilesModel->checkedItems.clear(); QModelIndexList indexlist; // Make a list of default master files @@ -337,12 +305,12 @@ void DataFilesDialog::restoreDefaults() dataFilesModel->setData(index, Qt::Checked, Qt::CheckStateRole); } } - dataFilesModel->submit(); // Force refresh of view + dataFilesModel->submit(); // Force refresh of view*/ } void DataFilesDialog::setCheckstate(QModelIndex index) { - // No check if index is valid: doubleclicked() always returns + /* No check if index is valid: doubleclicked() always returns // a valid index when emitted //index = QModelIndex(sortModel->mapToSource(index)); // Get a valid index @@ -357,11 +325,12 @@ void DataFilesDialog::setCheckstate(QModelIndex index) dataFilesModel->setData(index, Qt::Unchecked, Qt::CheckStateRole); } else { dataFilesModel->setData(index, Qt::Checked, Qt::CheckStateRole); - } + }*/ } void DataFilesDialog::setFilter() { + /* QStringList filefilter = (QStringList() << "*.esm" << "*.esp"); QStringList currentfilefilter; @@ -390,5 +359,5 @@ void DataFilesDialog::setFilter() // readConfig(); // dataFilesModel->submit(); // dataFilesModel->setData(); - +*/ } diff --git a/apps/launcher/datafilesdialog.h b/apps/launcher/datafilesdialog.h index 884de52191..d223bf14ad 100644 --- a/apps/launcher/datafilesdialog.h +++ b/apps/launcher/datafilesdialog.h @@ -1,20 +1,21 @@ #ifndef DATAFILESDIALOG_H #define DATAFILESDIALOG_H -#include -#include -#include -#include - #include "lineedit.h" class DataFilesModel; -class QStringList; -class QTableView; + +/*class QStringList; +class QTreeView; class QLineEdit; class QPlainTextEdit; class QItemSelectionModel; class QSortFilterProxyModel; +*/ +#include +#include + +class QTreeView; class DataFilesDialog : public QDialog { @@ -27,14 +28,14 @@ public: private: DataFilesModel *dataFilesModel; - QTableView *dataFilesView; - QItemSelectionModel *selectionModel; - QSortFilterProxyModel *sortModel; + QTreeView *dataFilesView; + //QItemSelectionModel *selectionModel; + //QSortFilterProxyModel *sortModel; - QLineEdit *lineAuthor; - LineEdit *lineFilter; - QPlainTextEdit *textDesc; - QPlainTextEdit *textDepends; + //QLineEdit *lineAuthor; + //LineEdit *lineFilter; + //QPlainTextEdit *textDesc; + //QPlainTextEdit *textDepends; public slots: diff --git a/apps/launcher/datafilesmodel.cpp b/apps/launcher/datafilesmodel.cpp index 99b924d67a..0eb68ab4c6 100644 --- a/apps/launcher/datafilesmodel.cpp +++ b/apps/launcher/datafilesmodel.cpp @@ -1,145 +1,155 @@ +#include + +#include "datafilesitem.h" #include "datafilesmodel.h" +//DataFilesModel::DataFilesModel(const QString &data, QObject *parent) DataFilesModel::DataFilesModel(QObject *parent) - : QFileSystemModel(parent) + : QAbstractItemModel(parent) { + QList rootData; + rootData << " "; + //rootItem = new DataFilesItem(rootData); + rootItem = new DataFilesItem(rootData); + //setupModelData(data.split(QString("\n")), rootItem); } -QVariant DataFilesModel::data(const QModelIndex &index, int role) const +DataFilesModel::~DataFilesModel() { - if (index.isValid() && role == Qt::DecorationRole) { - if (index.column() == 2) { - QFileIconProvider fip; - QIcon fileIcon = fip.icon(fileInfo(index)); - return fileIcon; - } - else { - return QIcon(); - } + delete rootItem; +} - } +int DataFilesModel::columnCount(const QModelIndex &parent) const +{ + if (parent.isValid()) + return static_cast(parent.internalPointer())->columnCount(); + else + return rootItem->columnCount(); +} - if (index.isValid() && role == Qt::DisplayRole) { - if (index.column() == 2) { - //qDebug() << index.data(Qt::DisplayRole); - if (fileInfo(index).suffix().toLower() == "esp") { - return QString("Plugin File"); - } - else if (fileInfo(index).suffix().toLower() == "esm") { - return QString("Master File"); +QVariant DataFilesModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); - } - } - } + if (role != Qt::DisplayRole) + return QVariant(); - if (index.isValid() && role == Qt::CheckStateRole && index.column() == 0) { - // Put a checkbox in the first column - if (index.isValid()) + DataFilesItem *item = static_cast(index.internalPointer()); - if (checkedItems.contains(filePath(index))) { -// if (checkedItems.contains(index)) { - return Qt::Checked; - } - else { - return Qt::Unchecked; - } - } - return QFileSystemModel::data(index, role); + return item->data(index.column()); } -bool DataFilesModel::setData(const QModelIndex& index, const QVariant& value, int role) +Qt::ItemFlags DataFilesModel::flags(const QModelIndex &index) const { + if (!index.isValid()) + return 0; - if (index.isValid() && role == Qt::CheckStateRole && index.column() == 0) { -// QPersistentModelIndex pindex(index); - - // qDebug() << pindex; - - if (value == Qt::Checked) { - //checkedItems.insert(pindex); - checkedItems.append(filePath(index)); - } else { -// checkedItems.remove(pindex); - checkedItems.removeAll(filePath(index)); - } + return Qt::ItemIsEnabled | Qt::ItemIsSelectable; +} - emit dataChanged(index, index); - return true; - } +QVariant DataFilesModel::headerData(int section, Qt::Orientation orientation, + int role) const +{ + if (orientation == Qt::Horizontal && role == Qt::DisplayRole) + return rootItem->data(section); - return QFileSystemModel::setData(index, value, role); + return QVariant(); } +QModelIndex DataFilesModel::index(int row, int column, const QModelIndex &parent) + const +{ + if (!hasIndex(row, column, parent)) + return QModelIndex(); + DataFilesItem *parentItem; -Qt::ItemFlags DataFilesModel::flags(const QModelIndex &index) const -{ - return QFileSystemModel::flags(index) | Qt::ItemIsUserCheckable; + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast(parent.internalPointer()); + + DataFilesItem *childItem = parentItem->child(row); + if (childItem) + return createIndex(row, column, childItem); + else + return QModelIndex(); } +QModelIndex DataFilesModel::parent(const QModelIndex &index) const +{ + if (!index.isValid()) + return QModelIndex(); + DataFilesItem *childItem = static_cast(index.internalPointer()); + DataFilesItem *parentItem = childItem->parent(); -/*QVariant DataFilesModel::headerData(int section, Qt::Orientation orientation, int role) const -{ - switch (role) { - case Qt::DecorationRole: - if (section == 0) { - // ### TODO oh man this is ugly and doesn't even work all the way! - // it is still 2 pixels off - QImage pixmap(16, 1, QImage::Format_Mono); - pixmap.fill(0); - pixmap.setAlphaChannel(pixmap.createAlphaMask()); - return pixmap; - } - break; - case Qt::TextAlignmentRole: - return Qt::AlignLeft; - } + if (parentItem == rootItem) + return QModelIndex(); - if (orientation != Qt::Horizontal || role != Qt::DisplayRole) - return QAbstractItemModel::headerData(section, orientation, role); - - QString returnValue; - switch (section) { - case 0: returnValue = tr("TES3 File"); - break; - case 1: returnValue = tr("Size"); - break; - case 2: returnValue = tr("Status"); - break; - case 3: returnValue = tr("Date Modified"); - break; - default: return QVariant(); - } - return returnValue; + return createIndex(parentItem->row(), 0, parentItem); } -*/ -/*test -void DataFilesModel::setCheckedItems(const QStringList &gameFiles) +int DataFilesModel::rowCount(const QModelIndex &parent) const { - qDebug() << "test"; - qDebug() << gameFiles.join("lol"); - + DataFilesItem *parentItem; + if (parent.column() > 0) + return 0; -}*/ + if (!parent.isValid()) + parentItem = rootItem; + else + parentItem = static_cast(parent.internalPointer()); -/*void DataFilesModel::unCheckAll() -{ - checkedItems.clear(); -// data(); - emit dataChanged(QModelIndex(), QModelIndex()); - submit(); -}*/ - -const QStringList DataFilesModel::getCheckedItems() -//const QList DataFilesModel::getCheckedItems() -//const QSet DataFilesModel::getCheckedItems() -{ - return checkedItems; + return parentItem->childCount(); } -/*void DataFilesModel::sort(int column, Qt::SortOrder order) +/*void DataFilesModel::setupModelData(const QStringList &lines, DataFilesItem *parent) { - qDebug() << "Sort!"; -}*/ + QList parents; + QList indentations; + parents << parent; + indentations << 0; + + int number = 0; + + while (number < lines.count()) { + int position = 0; + while (position < lines[number].length()) { + if (lines[number].mid(position, 1) != " ") + break; + position++; + } + + QString lineData = lines[number].mid(position).trimmed(); + + if (!lineData.isEmpty()) { + // Read the column data from the rest of the line. + QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts); + QList columnData; + for (int column = 0; column < columnStrings.count(); ++column) + columnData << columnStrings[column]; + + if (position > indentations.last()) { + // The last child of the current parent is now the new parent + // unless the current parent has no children. + + if (parents.last()->childCount() > 0) { + parents << parents.last()->child(parents.last()->childCount()-1); + indentations << position; + } + } else { + while (position < indentations.last() && parents.count() > 0) { + parents.pop_back(); + indentations.pop_back(); + } + } + + // Append a new item to the current parent's list of children. + parents.last()->appendChild(new DataFilesItem(columnData, parents.last())); + } + + number++; + } +}*/ \ No newline at end of file diff --git a/apps/launcher/datafilesmodel.h b/apps/launcher/datafilesmodel.h index 0e376865b7..bf2184dcdb 100644 --- a/apps/launcher/datafilesmodel.h +++ b/apps/launcher/datafilesmodel.h @@ -1,38 +1,35 @@ #ifndef DATAFILESMODEL_H #define DATAFILESMODEL_H -#include -#include +#include +#include +#include -#include +class DataFilesItem; -class DataFilesModel : public QFileSystemModel +class DataFilesModel : public QAbstractItemModel { + Q_OBJECT + public: + //DataFilesModel(const QString &data, QObject *parent = 0); DataFilesModel(QObject *parent = 0); - ~DataFilesModel() {}; - - QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + ~DataFilesModel(); + QVariant data(const QModelIndex &index, int role) const; Qt::ItemFlags flags(const QModelIndex &index) const; - - bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole); - -// void sort(int column, Qt::SortOrder order); - //test -// void setCheckedItems(const QStringList& files); -// void sort(int column, Qt::SortOrder order = Qt::AscendingOrder); -// void unCheckAll(); - -// const QSet getCheckedItems(); -// const QList getCheckedItems(); - const QStringList getCheckedItems(); - -// QVariant headerData(int section, Qt::Orientation orientation, int role) const; - -// QSet checkedItems; -// QList checkedItems; - QStringList checkedItems; + QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const; + QModelIndex index(int row, int column, + const QModelIndex &parent = QModelIndex()) const; + QModelIndex parent(const QModelIndex &index) const; + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &parent = QModelIndex()) const; + +private: + //void setupModelData(const QStringList &lines, TreeItem *parent); + + DataFilesItem *rootItem; }; -#endif +#endif \ No newline at end of file diff --git a/apps/launcher/launcher.pro b/apps/launcher/launcher.pro index ac17e019ef..4674c8c398 100644 --- a/apps/launcher/launcher.pro +++ b/apps/launcher/launcher.pro @@ -1,5 +1,5 @@ ###################################################################### -# Automatically generated by qmake (2.01a) Sun Mar 27 19:33:07 2011 +# Automatically generated by qmake (2.01a) Tue Mar 29 22:56:27 2011 ###################################################################### TEMPLATE = app @@ -9,15 +9,17 @@ INCLUDEPATH += . # Input HEADERS += datafilesdialog.h \ + datafilesitem.h \ datafilesmodel.h \ lineedit.h \ maindialog.h \ settingsdialog.h SOURCES += datafilesdialog.cpp \ + datafilesitem.cpp \ datafilesmodel.cpp \ lineedit.cpp \ main.cpp \ maindialog.cpp \ + qrc_resources.cxx \ settingsdialog.cpp RESOURCES += resources.qrc -RC_FILE = launcher.rc \ No newline at end of file