mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
replace QToolBox with VToolBox
This commit is contained in:
parent
6ac33d2bd0
commit
f032dede34
@ -96,7 +96,8 @@ SOURCES += main.cpp\
|
||||
vsnippetlist.cpp \
|
||||
vsnippet.cpp \
|
||||
dialog/veditsnippetdialog.cpp \
|
||||
utils/vimnavigationforwidget.cpp
|
||||
utils/vimnavigationforwidget.cpp \
|
||||
vtoolbox.cpp
|
||||
|
||||
HEADERS += vmainwindow.h \
|
||||
vdirectorytree.h \
|
||||
@ -179,7 +180,8 @@ HEADERS += vmainwindow.h \
|
||||
vsnippetlist.h \
|
||||
vsnippet.h \
|
||||
dialog/veditsnippetdialog.h \
|
||||
utils/vimnavigationforwidget.h
|
||||
utils/vimnavigationforwidget.h \
|
||||
vtoolbox.h
|
||||
|
||||
RESOURCES += \
|
||||
vnote.qrc \
|
||||
|
@ -31,6 +31,7 @@
|
||||
#include "vattachmentlist.h"
|
||||
#include "vfilesessioninfo.h"
|
||||
#include "vsnippetlist.h"
|
||||
#include "vtoolbox.h"
|
||||
|
||||
VMainWindow *g_mainWin;
|
||||
|
||||
@ -1200,7 +1201,7 @@ void VMainWindow::initDockWindows()
|
||||
// Snippets.
|
||||
m_snippetList = new VSnippetList(this);
|
||||
|
||||
m_toolBox = new QToolBox(this);
|
||||
m_toolBox = new VToolBox(this);
|
||||
m_toolBox->addItem(outline,
|
||||
QIcon(":/resources/icons/outline.svg"),
|
||||
tr("Outline"));
|
||||
|
@ -23,7 +23,7 @@ class VNotebook;
|
||||
class QActionGroup;
|
||||
class VFileList;
|
||||
class VEditArea;
|
||||
class QToolBox;
|
||||
class VToolBox;
|
||||
class VOutline;
|
||||
class VNotebookSelector;
|
||||
class VAvatar;
|
||||
@ -299,7 +299,7 @@ private:
|
||||
QDockWidget *toolDock;
|
||||
|
||||
// Tool box in the dock widget.
|
||||
QToolBox *m_toolBox;
|
||||
VToolBox *m_toolBox;
|
||||
|
||||
VOutline *outline;
|
||||
|
||||
|
96
src/vtoolbox.cpp
Normal file
96
src/vtoolbox.cpp
Normal file
@ -0,0 +1,96 @@
|
||||
#include "vtoolbox.h"
|
||||
|
||||
#include <QPushButton>
|
||||
#include <QStackedLayout>
|
||||
#include <QHBoxLayout>
|
||||
#include <QVBoxLayout>
|
||||
#include <QVariant>
|
||||
|
||||
|
||||
VToolBox::VToolBox(QWidget *p_parent)
|
||||
: QWidget(p_parent),
|
||||
m_currentIndex(-1)
|
||||
{
|
||||
setupUI();
|
||||
}
|
||||
|
||||
void VToolBox::setupUI()
|
||||
{
|
||||
m_btnLayout = new QHBoxLayout();
|
||||
m_btnLayout->addStretch();
|
||||
|
||||
m_widgetLayout = new QStackedLayout();
|
||||
|
||||
QVBoxLayout *mainLayout = new QVBoxLayout();
|
||||
mainLayout->addLayout(m_btnLayout);
|
||||
mainLayout->addLayout(m_widgetLayout);
|
||||
mainLayout->setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
setLayout(mainLayout);
|
||||
}
|
||||
|
||||
int VToolBox::addItem(QWidget *p_widget, const QIcon &p_iconSet, const QString &p_text)
|
||||
{
|
||||
int idx = m_items.size();
|
||||
|
||||
// New a button.
|
||||
QPushButton *btn = new QPushButton(p_iconSet, "");
|
||||
btn->setToolTip(p_text);
|
||||
btn->setProperty("FlatBtn", true);
|
||||
connect(btn, &QPushButton::clicked,
|
||||
this, [this]() {
|
||||
QObject *btn = sender();
|
||||
for (int i = 0; i < m_items.size(); ++i) {
|
||||
if (m_items[i].m_btn == btn) {
|
||||
setCurrentIndex(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
m_btnLayout->insertWidget(idx, btn);
|
||||
|
||||
// Insert widget to layout.
|
||||
m_widgetLayout->insertWidget(idx, p_widget);
|
||||
|
||||
m_items.push_back(ItemInfo(p_widget, p_text, btn));
|
||||
|
||||
if (m_items.size() == 1) {
|
||||
setCurrentIndex(0);
|
||||
}
|
||||
|
||||
return idx;
|
||||
}
|
||||
|
||||
void VToolBox::setCurrentIndex(int p_idx)
|
||||
{
|
||||
if (m_currentIndex == p_idx) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (p_idx < 0 || p_idx >= m_items.size()) {
|
||||
m_currentIndex = -1;
|
||||
} else {
|
||||
m_currentIndex = p_idx;
|
||||
}
|
||||
|
||||
setCurrentButtonIndex(m_currentIndex);
|
||||
|
||||
m_widgetLayout->setCurrentIndex(m_currentIndex);
|
||||
}
|
||||
|
||||
void VToolBox::setCurrentButtonIndex(int p_idx)
|
||||
{
|
||||
// Remove the text of all button.
|
||||
for (int i = 0; i < m_items.size(); ++i) {
|
||||
QPushButton *btn = m_items[i].m_btn;
|
||||
btn->setText("");
|
||||
btn->clearFocus();
|
||||
}
|
||||
|
||||
if (p_idx < 0 || p_idx >= m_items.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_items[p_idx].m_btn->setText(m_items[p_idx].m_text);
|
||||
}
|
57
src/vtoolbox.h
Normal file
57
src/vtoolbox.h
Normal file
@ -0,0 +1,57 @@
|
||||
#ifndef VTOOLBOX_H
|
||||
#define VTOOLBOX_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QIcon>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
class QPushButton;
|
||||
class QStackedLayout;
|
||||
class QBoxLayout;
|
||||
|
||||
class VToolBox : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit VToolBox(QWidget *p_parent = nullptr);
|
||||
|
||||
int addItem(QWidget *p_widget, const QIcon &p_iconSet, const QString &p_text);
|
||||
|
||||
void setCurrentIndex(int p_idx);
|
||||
|
||||
private:
|
||||
struct ItemInfo
|
||||
{
|
||||
ItemInfo()
|
||||
: m_widget(nullptr), m_btn(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
ItemInfo(QWidget *p_widget,
|
||||
const QString &p_text,
|
||||
QPushButton *p_btn)
|
||||
: m_widget(p_widget), m_text(p_text), m_btn(p_btn)
|
||||
{
|
||||
}
|
||||
|
||||
QWidget *m_widget;
|
||||
QString m_text;
|
||||
|
||||
QPushButton *m_btn;
|
||||
};
|
||||
|
||||
void setupUI();
|
||||
|
||||
void setCurrentButtonIndex(int p_idx);
|
||||
|
||||
QBoxLayout *m_btnLayout;
|
||||
|
||||
QStackedLayout *m_widgetLayout;
|
||||
|
||||
int m_currentIndex;
|
||||
|
||||
QVector<ItemInfo> m_items;
|
||||
};
|
||||
|
||||
#endif // VTOOLBOX_H
|
Loading…
x
Reference in New Issue
Block a user