2017-01-05 03:05:08 +00:00
|
|
|
#include "VersionSelectWidget.h"
|
2021-11-22 02:55:16 +00:00
|
|
|
|
2017-01-05 03:05:08 +00:00
|
|
|
#include <QProgressBar>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
#include <QHeaderView>
|
2021-11-22 02:55:16 +00:00
|
|
|
|
|
|
|
#include "VersionListView.h"
|
|
|
|
#include "VersionProxyModel.h"
|
|
|
|
|
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
2017-01-05 03:05:08 +00:00
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
VersionSelectWidget::VersionSelectWidget(QWidget* parent)
|
2018-07-15 12:51:05 +00:00
|
|
|
: QWidget(parent)
|
2017-01-05 03:05:08 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
setObjectName(QStringLiteral("VersionSelectWidget"));
|
|
|
|
verticalLayout = new QVBoxLayout(this);
|
|
|
|
verticalLayout->setObjectName(QStringLiteral("verticalLayout"));
|
|
|
|
verticalLayout->setContentsMargins(0, 0, 0, 0);
|
2017-01-05 03:05:08 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
m_proxyModel = new VersionProxyModel(this);
|
2017-01-05 03:05:08 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
listView = new VersionListView(this);
|
|
|
|
listView->setObjectName(QStringLiteral("listView"));
|
|
|
|
listView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
listView->setAlternatingRowColors(true);
|
|
|
|
listView->setRootIsDecorated(false);
|
|
|
|
listView->setItemsExpandable(false);
|
|
|
|
listView->setWordWrap(true);
|
|
|
|
listView->header()->setCascadingSectionResizes(true);
|
|
|
|
listView->header()->setStretchLastSection(false);
|
|
|
|
listView->setModel(m_proxyModel);
|
|
|
|
verticalLayout->addWidget(listView);
|
2017-01-05 03:05:08 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
sneakyProgressBar = new QProgressBar(this);
|
|
|
|
sneakyProgressBar->setObjectName(QStringLiteral("sneakyProgressBar"));
|
|
|
|
sneakyProgressBar->setFormat(QStringLiteral("%p%"));
|
|
|
|
verticalLayout->addWidget(sneakyProgressBar);
|
|
|
|
sneakyProgressBar->setHidden(true);
|
|
|
|
connect(listView->selectionModel(), &QItemSelectionModel::currentRowChanged, this, &VersionSelectWidget::currentRowChanged);
|
2017-01-05 03:05:08 +00:00
|
|
|
|
2018-07-15 12:51:05 +00:00
|
|
|
QMetaObject::connectSlotsByName(this);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 00:30:23 +00:00
|
|
|
void VersionSelectWidget::setCurrentVersion(const QString& version)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_currentVersion = version;
|
2018-11-09 19:44:39 +00:00
|
|
|
m_proxyModel->setCurrentVersion(version);
|
2017-12-09 00:30:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 03:05:08 +00:00
|
|
|
void VersionSelectWidget::setEmptyString(QString emptyString)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
listView->setEmptyString(emptyString);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::setEmptyErrorString(QString emptyErrorString)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
listView->setEmptyErrorString(emptyErrorString);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VersionSelectWidget::~VersionSelectWidget()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::setResizeOn(int column)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
listView->header()->setSectionResizeMode(resizeOnColumn, QHeaderView::ResizeToContents);
|
|
|
|
resizeOnColumn = column;
|
|
|
|
listView->header()->setSectionResizeMode(resizeOnColumn, QHeaderView::Stretch);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
void VersionSelectWidget::initialize(BaseVersionList *vlist)
|
2017-01-05 03:05:08 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_vlist = vlist;
|
|
|
|
m_proxyModel->setSourceModel(vlist);
|
|
|
|
listView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
listView->header()->setSectionResizeMode(resizeOnColumn, QHeaderView::Stretch);
|
|
|
|
|
|
|
|
if (!m_vlist->isLoaded())
|
|
|
|
{
|
|
|
|
loadList();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (m_proxyModel->rowCount() == 0)
|
|
|
|
{
|
|
|
|
listView->setEmptyMode(VersionListView::String);
|
|
|
|
}
|
|
|
|
preselect();
|
|
|
|
}
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::closeEvent(QCloseEvent * event)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
QWidget::closeEvent(event);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::loadList()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto newTask = m_vlist->getLoadTask();
|
|
|
|
if (!newTask)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
loadTask = newTask.get();
|
|
|
|
connect(loadTask, &Task::succeeded, this, &VersionSelectWidget::onTaskSucceeded);
|
|
|
|
connect(loadTask, &Task::failed, this, &VersionSelectWidget::onTaskFailed);
|
|
|
|
connect(loadTask, &Task::progress, this, &VersionSelectWidget::changeProgress);
|
|
|
|
if(!loadTask->isRunning())
|
|
|
|
{
|
|
|
|
loadTask->start();
|
|
|
|
}
|
|
|
|
sneakyProgressBar->setHidden(false);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 22:58:54 +00:00
|
|
|
void VersionSelectWidget::onTaskSucceeded()
|
2017-01-05 03:05:08 +00:00
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if (m_proxyModel->rowCount() == 0)
|
|
|
|
{
|
|
|
|
listView->setEmptyMode(VersionListView::String);
|
|
|
|
}
|
|
|
|
sneakyProgressBar->setHidden(true);
|
|
|
|
preselect();
|
|
|
|
loadTask = nullptr;
|
2017-03-19 22:58:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::onTaskFailed(const QString& reason)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
CustomMessageBox::selectable(this, tr("Error"), tr("List update failed:\n%1").arg(reason), QMessageBox::Warning)->show();
|
|
|
|
onTaskSucceeded();
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::changeProgress(qint64 current, qint64 total)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
sneakyProgressBar->setMaximum(total);
|
|
|
|
sneakyProgressBar->setValue(current);
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::currentRowChanged(const QModelIndex& current, const QModelIndex&)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto variant = m_proxyModel->data(current, BaseVersionList::VersionPointerRole);
|
|
|
|
emit selectedVersionChanged(variant.value<BaseVersionPtr>());
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::preselect()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if(preselectedAlready)
|
|
|
|
return;
|
|
|
|
selectCurrent();
|
|
|
|
if(preselectedAlready)
|
|
|
|
return;
|
|
|
|
selectRecommended();
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
2017-12-09 00:30:23 +00:00
|
|
|
void VersionSelectWidget::selectCurrent()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
if(m_currentVersion.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto idx = m_proxyModel->getVersion(m_currentVersion);
|
|
|
|
if(idx.isValid())
|
|
|
|
{
|
|
|
|
preselectedAlready = true;
|
|
|
|
listView->selectionModel()->setCurrentIndex(idx,QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
|
|
|
|
listView->scrollTo(idx, QAbstractItemView::PositionAtCenter);
|
|
|
|
}
|
2017-12-09 00:30:23 +00:00
|
|
|
}
|
|
|
|
|
2017-01-05 03:05:08 +00:00
|
|
|
void VersionSelectWidget::selectRecommended()
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto idx = m_proxyModel->getRecommended();
|
|
|
|
if(idx.isValid())
|
|
|
|
{
|
|
|
|
preselectedAlready = true;
|
|
|
|
listView->selectionModel()->setCurrentIndex(idx,QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);
|
|
|
|
listView->scrollTo(idx, QAbstractItemView::PositionAtCenter);
|
|
|
|
}
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool VersionSelectWidget::hasVersions() const
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
return m_proxyModel->rowCount(QModelIndex()) != 0;
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BaseVersionPtr VersionSelectWidget::selectedVersion() const
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
auto currentIndex = listView->selectionModel()->currentIndex();
|
|
|
|
auto variant = m_proxyModel->data(currentIndex, BaseVersionList::VersionPointerRole);
|
|
|
|
return variant.value<BaseVersionPtr>();
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::setExactFilter(BaseVersionList::ModelRoles role, QString filter)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_proxyModel->setFilter(role, new ExactFilter(filter));
|
2017-01-05 03:05:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::setFuzzyFilter(BaseVersionList::ModelRoles role, QString filter)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_proxyModel->setFilter(role, new ContainsFilter(filter));
|
2018-03-19 01:36:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VersionSelectWidget::setFilter(BaseVersionList::ModelRoles role, Filter *filter)
|
|
|
|
{
|
2018-07-15 12:51:05 +00:00
|
|
|
m_proxyModel->setFilter(role, filter);
|
2017-04-01 16:38:04 +00:00
|
|
|
}
|