添加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

@@ -1,6 +0,0 @@
#ifndef SPKCONFIG_H
#define SPKCONFIG_H
#endif // SPKCONFIG_H

View File

@@ -41,4 +41,4 @@ class SpkLogger
#define sErr(X) SpkLogger::GetInstance()->Error(X)
#define sErrPop(X) SpkLogger::GetInstance()->Error(X,true)
#define sCritical(X) SpkLogger::GetInstance()->Critical(X)
#define sNotify(X)
#define sNotify(X) SpkLogger::GetInstance()->Notify(X)

View File

@@ -9,9 +9,12 @@
#include <QStackedWidget>
#include <QButtonGroup>
#include <QJsonObject>
#include <QTreeWidget>
#include "spksidebartree.h" // In place of #include <QTreeWidget>
#include <QPointer>
#include "spkpageqsstest.h"
class QNetworkReply;
namespace SpkUi
{
class SpkSidebarSelector : public QObject
@@ -21,6 +24,7 @@ namespace SpkUi
QPushButton *mLastCheckedBtn;
QTreeWidgetItem *mLastSelectedItem;
QTreeWidget *mCategoryWidget;
QVector<QTreeWidgetItem *> mUnusableItems; // Unselectable top level items; never changes
public:
SpkSidebarSelector(QObject *parent = nullptr) : QObject(parent)
@@ -40,9 +44,10 @@ namespace SpkUi
void BindCategoryWidget(QTreeWidget* w)
{
mCategoryWidget = w;
connect(w, &QTreeWidget::itemPressed, this,
connect(w, &QTreeWidget::itemClicked, this,
&SpkSidebarSelector::TreeItemSelected);
}
void AddUnusableItem(QTreeWidgetItem *i) { mUnusableItems.append(i); }
private slots:
// We assume the objects in interest all have the correct properties
@@ -73,6 +78,10 @@ namespace SpkUi
}
void TreeItemSelected(QTreeWidgetItem *item, int column)
{
if(mUnusableItems.contains(item))
{
UnusableItemSelected(item); return;
}
if(mLastCheckedBtn)
{
mLastCheckedBtn->setChecked(false);
@@ -84,6 +93,18 @@ namespace SpkUi
else
emit SwitchToPage(item->data(column, RoleItemCategoryPageId).toInt());
}
void UnusableItemSelected(QTreeWidgetItem *i)
{
i->setSelected(false);
if(mLastSelectedItem)
{
mLastSelectedItem->setSelected(true);
}
else if(mLastCheckedBtn)
{
mLastCheckedBtn->setChecked(true);
}
}
signals:
void SwitchToCategory(int aCategoryId);
@@ -109,10 +130,12 @@ namespace SpkUi
QHBoxLayout *HLaySideTop;
QLabel *StoreIcon;
QPushButton *BtnSettings, *BtnFeedback, *BtnLogs;
QTreeWidget *CategoryWidget;
SpkSidebarTree *CategoryWidget;
QMap<int, QTreeWidgetItem> *CategoryItemMap;
SpkSidebarSelector *SidebarMgr;
QTreeWidgetItem *CategoryParentItem;
//Pages
SpkPageQssTest *PageQssTest;
};
@@ -127,5 +150,14 @@ class SpkMainWindow : public SpkWindow
public:
SpkMainWindow(QWidget *parent = nullptr);
void PopulateCategories(QJsonObject);
void PopulateCategories(QJsonArray);
private:
QPointer<QNetworkReply> mCategoryGetReply;
public slots:
void RefreshCategoryData();
private slots:
void CategoryDataReceived();
};

View File

@@ -10,7 +10,8 @@ class SpkMsgBox : public SpkDialog
public:
SpkMsgBox(QWidget *parent = nullptr);
static int StaticExec(QString msg, QString title, QMessageBox::Icon = QMessageBox::NoIcon,
QMessageBox::StandardButtons = QMessageBox::Ok, QString extra = "");
QMessageBox::StandardButtons = QMessageBox::Ok, QString extra = "",
bool expanded = false);
private:
static void AddButtons(SpkMsgBox *me, QMessageBox::StandardButtons b);
QList<QMessageBox::StandardButton> mButtonList;

17
inc/spksidebartree.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include <QTreeWidget>
namespace SpkUi
{
class SpkSidebarTree : public QTreeWidget
{
Q_OBJECT
public:
SpkSidebarTree(QWidget* parent = nullptr);
protected:
void mouseMoveEvent(QMouseEvent *) override;
void mousePressEvent(QMouseEvent *) override;
};
}

View File

@@ -1,8 +1,9 @@
#pragma once
#include <QMap>
#include <QJsonDocument>
#include <QString>
#include <QSettings>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkAccessManager>
@@ -20,35 +21,19 @@ class SpkStore : public QObject
Q_OBJECT
public:
static SpkStore *Instance;
QSettings *mCfg;
SpkStore(bool aCli, QString &aLogPath);
~SpkStore();
SpkMainWindow* GetRootWindow() { return mMainWindow; }
void SetApiResuestUrl(QString aUrlStr) { mApiRequestUrl = aUrlStr; }
QString GetApiRequestUrl() { return mApiRequestUrl; }
QNetworkReply *SendApiRequest(QString path, QJsonDocument param = QJsonDocument());
private:
SpkLogger *mLogger;
SpkMainWindow *mMainWindow = nullptr;
QNetworkAccessManager *mNetMgr = nullptr;
// Following are stationary signal-slot bindings between UI and Store, mostly for handling
// API calls and resource downloading.
public slots:
// void RequestStoreMetadata(); ///< All required metadata the store needs when launched
// void RequestCategoryPage(int aCategoryId);
// void RequestApplicationMetadata(int aAppId);
// void RequestRefreshApiUrls(QString aCustomUrl);
signals:
void StatusStoreMetadata(QNetworkReply::NetworkError, QString);
void StatusCategoryPage(QNetworkReply::NetworkError, QString);
void StatusApplicationMetadata(QNetworkReply::NetworkError, QString);
void StatusRefreshApiUrls(QNetworkReply::NetworkError, QString);
private:
// Store manages all kinds of possible replies, and the caller can only get JSON they need
QNetworkReply *mReplyStoreMetadata = nullptr,
*mReplyCategory = nullptr,
*mReplyAppMetadata = nullptr,
*mReplyApiUrls = nullptr;
QString mDistroName, mApiRequestUrl, mUserAgentStr;
};

25
inc/spkutils.h Normal file
View File

@@ -0,0 +1,25 @@
#pragma once
#include <QPointer>
#include <QString>
#include <QSettings>
#include <QFile>
#include <QtNetwork/QNetworkReply>
#include "spkstore.h"
#include "spklogging.h"
#define STORE SpkStore::Instance
#define CFG (SpkStore::Instance->mCfg)
namespace SpkUtils
{
QString GetDistroName();
void VerifySingleRequest(QPointer<QNetworkReply> aReply);
void DeleteReplyLater(QNetworkReply *aReply);
bool VerifyReplyJson(QNetworkReply *aReply, QJsonValue& aRetDoc);
}