AllNoteButtonTreeDelegateEditor和EditorSettingsOptions

develop
Sunyuxuan 1 month ago
parent 9a666c1b07
commit d0f348a7f9

@ -7,7 +7,7 @@
#include "notelistview.h"
#include "editorsettingsoptions.h"
#include "fontloader.h"
//分操作系统设置font
AllNoteButtonTreeDelegateEditor::AllNoteButtonTreeDelegateEditor(QTreeView *view,
const QStyleOptionViewItem &option,
const QModelIndex &index,
@ -45,20 +45,27 @@ AllNoteButtonTreeDelegateEditor::AllNoteButtonTreeDelegateEditor(QTreeView *view
{
setContentsMargins(0, 0, 0, 0);
}
//重写paintEvent函数
void AllNoteButtonTreeDelegateEditor::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//定图标位置
auto iconRect = QRect(rect().x() + 22, rect().y() + (rect().height() - 20) / 2, 18, 20);
//获取图标路径
auto iconPath = m_index.data(NodeItem::Roles::Icon).toString();
//获取显示文本
auto displayName = m_index.data(NodeItem::Roles::DisplayText).toString();
QRect nameRect(rect());
nameRect.setLeft(iconRect.x() + iconRect.width() + 5);
nameRect.setWidth(nameRect.width() - 22 - 40);
if (m_view->selectionModel()->isSelected(m_index)) {
//根据是否被选中绘制不同颜色
painter.fillRect(rect(), QBrush(m_activeColor));
painter.setPen(m_titleSelectedColor);
} else {
//拖动绘制
auto listView = dynamic_cast<NoteListView *>(m_listView);
if (listView->isDragging()) {
if (m_theme == Theme::Dark) {
@ -71,6 +78,7 @@ void AllNoteButtonTreeDelegateEditor::paintEvent(QPaintEvent *event)
}
painter.setPen(m_folderIconColor);
}
//又开始分操作系统绘制
#ifdef __APPLE__
int iconPointSizeOffset = 0;
#else
@ -86,6 +94,7 @@ void AllNoteButtonTreeDelegateEditor::paintEvent(QPaintEvent *event)
painter.setPen(m_titleColor);
}
//设置子项
painter.setFont(m_titleFont);
painter.drawText(nameRect, Qt::AlignLeft | Qt::AlignVCenter, displayName);
auto childCountRect = rect();
@ -100,9 +109,12 @@ void AllNoteButtonTreeDelegateEditor::paintEvent(QPaintEvent *event)
painter.setFont(m_numberOfNotesFont);
painter.drawText(childCountRect, Qt::AlignHCenter | Qt::AlignVCenter,
QString::number(childCount));
//调用基类 QWidget 的 paintEvent 函数,以确保小部件的其他默认绘制行为得以执行
QWidget::paintEvent(event);
}
//又是用switch来设置枚举类型的值
//要注意的一点是没有使用0、1、2等来列举而是使用key值来列举有利于增加代码规模和程序员阅读
void AllNoteButtonTreeDelegateEditor::setTheme(Theme::Value theme)
{
m_theme = theme;

@ -2,12 +2,17 @@
#define ALLNOTEBUTTONTREEDELEGATEEDITOR_H
#include <QWidget>
//用来描述书图像的显示选项
#include <QStyleOptionViewItem>
//用于在模型/视图架构中索引项
#include <QModelIndex>
//用于设置和管理字体
#include <QFont>
#include "editorsettingsoptions.h"
//用于显示树形视图的组件
class QTreeView;
//用于显示列表视图的组件
class QListView;
class AllNoteButtonTreeDelegateEditor : public QWidget
@ -37,6 +42,7 @@ private:
Theme::Value m_theme;
// QWidget interface
protected:
//重写QWidget的paintEvent函数用于自定义绘制编辑器小部件的内容
virtual void paintEvent(QPaintEvent *event) override;
};

@ -3,8 +3,10 @@
#include <QVariant>
#include <qdebug.h>
// 重载<<运算符,将枚举类型的值输出到标准输出流
EditorSettingsOptions::EditorSettingsOptions(QObject *) { }
//选用switch来选择要输出的枚举值
std::ostream &operator<<(std::ostream &os, const FontTypeface::Value &fontTypeface)
{
switch (fontTypeface) {
@ -37,6 +39,7 @@ std::ostream &operator<<(std::ostream &os, const Theme::Value &theme)
return os;
}
//使用str()函数来重载to_string函数
std::string to_string(FontTypeface::Value fontTypeface)
{
std::ostringstream oss;
@ -51,11 +54,13 @@ std::string to_string(Theme::Value theme)
return oss.str();
}
//函数调用,没看懂什么意思,是要再进行功能扩展吗
void setCSSThemeAndUpdate(QWidget *obj, Theme::Value theme)
{
//标准输入输出
setCSSClassesAndUpdate(obj, QString::fromStdString(to_string(theme)).toLower().toStdString());
}
//更新
void setCSSClassesAndUpdate(QWidget *obj, std::string classNames)
{
if (obj->styleSheet().isEmpty()) {

@ -2,12 +2,13 @@
#define EDITORSETTINGSOPTIONS_H
#include <QObject>
//声明QtQml/qqml.h库这个库包含Qt QML相关文件可将C++类暴露给QML
#include <QtQml/qqml.h>
#include <sstream>
#include <QString>
#include <QWidget>
#include "lqtutils_enum.h"
//声明枚举类型
L_DECLARE_ENUM(FontTypeface, SansSerif, Serif, Mono)
L_DECLARE_ENUM(FontSizeAction, FontSizeIncrease, FontSizeDecrease)
L_DECLARE_ENUM(EditorTextWidth, TextWidthIncrease, TextWidthDecrease, TextWidthFullWidth)
@ -22,11 +23,14 @@ public:
explicit EditorSettingsOptions(QObject *parent = nullptr);
};
//重载<<运算符,将枚举类型的值输出到标准输出流
std::ostream &operator<<(std::ostream &os, const Theme::Value &theme);
std::ostream &operator<<(std::ostream &os, const FontTypeface::Value &fontTypeface);
//为了适配标准输出流,重载字符串转换函数
std::string to_string(FontTypeface::Value fontTypeface);
std::string to_string(Theme::Value theme);
//更新CSS样式和类
void setCSSThemeAndUpdate(QWidget *obj, Theme::Value theme);
void setCSSClassesAndUpdate(QWidget *obj, std::string classNames);

Loading…
Cancel
Save