quake-tools/source/quam/project.cc

116 lines
2.6 KiB
C++

#include "quam/project.h"
#include <QErrorMessage>
#include <QSortFilterProxyModel>
Project::Project(std::istream &st, QErrorMessage *errors, QWidget *parent) :
QWidget{parent},
Ui::Project{},
m_seekingDir{false},
m_histPos{0},
m_hist(),
m_arc{new Arc::Arc{st, this}},
m_errors{errors},
m_model{new Arc::Model{this}},
m_sorter{new QSortFilterProxyModel{this}}
{
setupUi(this);
connect(m_model, &Arc::Model::dirChanged,
this, &Project::dirChanged);
connect(tableView, &QAbstractItemView::doubleClicked,
m_model, &Arc::Model::setDirToIndex);
connect(dirName, &QLineEdit::returnPressed,
this, &Project::goPath);
dirName->setValidator(m_arc->validator);
m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter);
seekTop();
}
Project::~Project() {
}
bool Project::seekHistory(std::ptrdiff_t newPos) {
if(newPos < 0 || newPos >= m_hist.size()) {
return false;
}
m_histPos = newPos;
auto newPath = m_hist.at(newPos);
if(auto node = m_model->findPath(newPath)) {
if(auto dir = node->getDir()) {
m_seekingDir = true;
m_model->setDir(dir);
return true;
} else {
m_errors->showMessage(QString::fromStdString("'"s + newPath + "' ") +
tr("is not a directory"));
return false;
}
} else {
m_errors->showMessage(tr("could not find directory") +
QString::fromStdString(" '"s + newPath + "'"));
return false;
}
}
bool Project::seekPath(std::string path) {
return m_model->goPath(path);
}
void Project::seekTop() {
m_model->setDir(&m_arc->root);
}
bool Project::goBack() {
return seekHistory(std::ptrdiff_t(m_histPos) - 1);
}
bool Project::goForward() {
return seekHistory(std::ptrdiff_t(m_histPos) + 1);
}
bool Project::goTop() {
seekTop();
return true;
}
bool Project::goUp() {
return m_model->goUp();
}
bool Project::goPath() {
auto newPath = dirName->text().toStdString();
if(!m_model->goPath(newPath)) {
m_errors->showMessage(tr("could not find directory") +
QString::fromStdString(" '"s + newPath + "'"));
return false;
} else {
return true;
}
}
void Project::dirChanged(Arc::Dir *from, Arc::Dir *to) {
if(!m_seekingDir) {
if(std::ptrdiff_t(m_histPos) < std::ptrdiff_t(m_hist.size()) - 1) {
auto beg = m_hist.begin();
std::advance(beg, m_histPos + 1);
m_hist.erase(beg, m_hist.end());
}
m_hist.push_back(to->getPath());
m_histPos = m_hist.size() - 1;
} else {
m_seekingDir = false;
}
dirName->setText(QString::fromStdString(to->getPath()));
tableView->sortByColumn(int(Arc::Column::Type), Qt::AscendingOrder);
tableView->resizeColumnsToContents();
}
// EOF