添加API调用接口,添加读取分类,添加SpkUtils实用函数

使用了SpkSidebarTree子类实现对TreeWidget的特殊要求:不能取消选择,不能拖拽选择。

API调用接口暂时写死。API会获取

配置文件已添加但暂不使用。
This commit is contained in:
RigoLigoRLC
2021-07-17 19:22:31 +08:00
parent 48c9046993
commit e3c43198b9
17 changed files with 317 additions and 110 deletions

View File

@@ -5,6 +5,7 @@
#include "spkmsgbox.h"
#include "spkmainwindow.h"
#include "spklogging.h"
#include "spkutils.h"
#include "spkuimsg.h"
SpkMainWindow::SpkMainWindow(QWidget *parent) : SpkWindow(parent)
@@ -14,47 +15,23 @@ SpkMainWindow::SpkMainWindow(QWidget *parent) : SpkWindow(parent)
SetUseTitleBar(false);
SetCentralWidget(ui);
SetTitleBar(ui->TitleBar, false);
RefreshCategoryData();
auto size = QGuiApplication::primaryScreen()->size() * 0.25;
resize(QGuiApplication::primaryScreen()->size() * 0.5);
move(size.width(), size.height());
}
void SpkMainWindow::PopulateCategories(QJsonObject aCategoryData)
void SpkMainWindow::PopulateCategories(QJsonArray aCategoryData)
{
using SpkUi::SpkSidebarSelector;
QTreeWidgetItem *catg;
auto w = ui->CategoryWidget;
if(!aCategoryData.contains("code"))
{
SpkUiMessage::SendStoreNotification(tr("Failed to load categories; return code lost."));
return;
}
auto OpRetCode = aCategoryData.value("code");
if(!OpRetCode.isDouble())
{
SpkUiMessage::SendStoreNotification(tr("Failed to load categories; invalid return code."));
return;
}
if(OpRetCode.toInt() != 0)
{
SpkUiMessage::SendStoreNotification(tr("Failed to load categories; operation failed: %1.")
.arg(OpRetCode.toDouble()));
return;
}
if(ui->CategoryParentItem->childCount()) // Clear all existing children if there is any
foreach(auto &i, ui->CategoryParentItem->takeChildren())
delete i;
if(!aCategoryData.contains("data"))
{
SpkUiMessage::SendStoreNotification(tr("Failed to load categories; data lost."));
return;
}
auto OpData = aCategoryData.value("data");
if(!OpRetCode.isArray())
{
SpkUiMessage::SendStoreNotification(tr("Failed to load categories; invalid data."));
return;
}
auto OpDataArr = OpData.toArray();
foreach(auto i, OpDataArr)
foreach(auto i, aCategoryData)
{
if(i.isObject())
{
@@ -67,20 +44,41 @@ void SpkMainWindow::PopulateCategories(QJsonObject aCategoryData)
if(j.contains("type_name") && j.value("type_name").isString())
typeName = j.value("type_name").toString();
else goto WRONG_CATEGORY;
// TODO
catg = new QTreeWidgetItem(ui->CategoryParentItem, QStringList(typeName));
catg->setData(0, SpkSidebarSelector::RoleItemIsCategory, true);
catg->setData(0, SpkSidebarSelector::RoleItemCategoryPageId, typeId);
continue;
WRONG_CATEGORY:;
}
WRONG_CATEGORY:
sLog("One category ignored because of invalid data");
ui->CategoryParentItem->setExpanded(true);
}
}
void SpkMainWindow::RefreshCategoryData()
{
// Asynchronously call category API
using namespace SpkUtils;
VerifySingleRequest(mCategoryGetReply);
mCategoryGetReply = STORE->SendApiRequest("type/get_type_list");
DeleteReplyLater(mCategoryGetReply);
connect(mCategoryGetReply, &QNetworkReply::finished, this, &SpkMainWindow::CategoryDataReceived);
}
void SpkMainWindow::CategoryDataReceived()
{
QJsonValue retval;
if(!SpkUtils::VerifyReplyJson(mCategoryGetReply, retval) || !retval.isArray())
{
sErr(tr("Failed to load categories!"));
// TODO: Switch to an error page
}
PopulateCategories(retval.toArray());
}
SpkUi::SpkMainWidget::SpkMainWidget(QWidget *parent) : QFrame(parent)
{
setObjectName("spk_mainwidget");
QTreeWidgetItem *item;
Pager = new QStackedWidget(this);
Pager->setObjectName("spk_mw_pager");
Pager->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
@@ -140,16 +138,19 @@ SpkUi::SpkMainWidget::SpkMainWidget(QWidget *parent) : QFrame(parent)
HLaySideTop->addWidget(BtnSettings);
VLaySidebar->addLayout(HLaySideTop);
CategoryWidget = new QTreeWidget(this);
using SpkUi::SpkSidebarSelector;
CategoryWidget = new SpkSidebarTree(this);
CategoryWidget->setObjectName("spk_mw_category");
CategoryWidget->setAutoFillBackground(true);
CategoryWidget->setColumnCount(1);
CategoryWidget->setHeaderHidden(true);
CategoryWidget->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);
item = new QTreeWidgetItem(QStringList("Placeholder"));
item->setData(0, Qt::UserRole + 1, 1);
item->setData(0, Qt::UserRole + 2, 1);
CategoryWidget->addTopLevelItem(item);
CategoryParentItem = new QTreeWidgetItem(QStringList(tr("Categories")));
CategoryParentItem->setFlags(CategoryParentItem->flags().setFlag(Qt::ItemIsSelectable, false));
CategoryParentItem->setExpanded(true);
SidebarMgr->AddUnusableItem(CategoryParentItem);
CategoryWidget->addTopLevelItem(CategoryParentItem);
// FIXMEIFPOSSIBLE: Fusion adds extra gradient.
// Details: https://forum.qt.io/topic/128190/fusion-style-kept-adding-an-extra-
// layer-of-gradient-to-my-selected-item-of-qtreewidget-even-with-qss

View File

@@ -19,7 +19,7 @@ SpkMsgBox::SpkMsgBox(QWidget *parent)
}
int SpkMsgBox::StaticExec(QString msg, QString title, QMessageBox::Icon icon,
QMessageBox::StandardButtons buttons, QString extra)
QMessageBox::StandardButtons buttons, QString extra, bool expanded)
{
SpkMsgBox *b = new SpkMsgBox(SpkStore::Instance->GetRootWindow());
QWidget *wMsgWidget = new QWidget;
@@ -98,8 +98,12 @@ int SpkMsgBox::StaticExec(QString msg, QString title, QMessageBox::Icon icon,
b->AddSpacing(3);
AddButtons(b, buttons);
if(hasextra) // Keep conventional buttons centered
b->mBtnLay->addStretch();
if(hasextra)
{
b->mBtnLay->addStretch(); // Keep conventional buttons centered
if(expanded)
emit wExpandBtn->clicked();
}
InitialHeight = b->minimumSizeHint().height();
auto pos = (SpkUi::PrimaryScreenSize - b->sizeHint()) / 2;
b->move(pos.width(), pos.height());

31
gui/spksidebartree.cpp Normal file
View File

@@ -0,0 +1,31 @@
#include <QMouseEvent>
#include "spksidebartree.h"
SpkUi::SpkSidebarTree::SpkSidebarTree(QWidget *parent) :
QTreeWidget(parent)
{
}
void SpkUi::SpkSidebarTree::mouseMoveEvent(QMouseEvent *e)
{
// This is solely for forcibly disabling the view to change selection when dragging on the view
// and probably the only reason why this class began its existence
if((e->buttons() & Qt::LeftButton))
setState(NoState);
else
QTreeWidget::mouseMoveEvent(e);
}
void SpkUi::SpkSidebarTree::mousePressEvent(QMouseEvent *e)
{
// Prevent anything being deselected
if(e->modifiers().testFlag(Qt::ControlModifier) && e->buttons().testFlag(Qt::LeftButton))
{
auto i = itemAt(e->pos());
if(i && i->isSelected())
return;
}
QTreeWidget::mousePressEvent(e);
}

View File

@@ -115,7 +115,7 @@ namespace SpkUi
// FIXME: Chameleon style kept adding unwanted blue focus indication border
// to widgets that shouldn't have borders.
// We need to eliminate this irritating problem.
if(qgetenv("SPARK_NO_QSTYLE_CHANGE") == "1")
if(qgetenv("SPARK_NO_QSTYLE_CHANGE").toInt())
return;
OldSystemStyle = QStyleFactory::create("chameleon"); // TreeWidget doesn't work well with Fusion
auto styles = QStyleFactory::keys();