mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
support avatar
Signed-off-by: Le Tan <tamlokveer@gmail.com>
This commit is contained in:
parent
8f866f34ff
commit
edca922f5e
@ -140,3 +140,4 @@ QToolBox::tab {
|
|||||||
QWidget[NotebookPanel="true"] {
|
QWidget[NotebookPanel="true"] {
|
||||||
padding-left: 3px;
|
padding-left: 3px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,8 @@ SOURCES += main.cpp\
|
|||||||
vdirectory.cpp \
|
vdirectory.cpp \
|
||||||
vfile.cpp \
|
vfile.cpp \
|
||||||
vnotebookselector.cpp \
|
vnotebookselector.cpp \
|
||||||
vnofocusitemdelegate.cpp
|
vnofocusitemdelegate.cpp \
|
||||||
|
vavatar.cpp
|
||||||
|
|
||||||
HEADERS += vmainwindow.h \
|
HEADERS += vmainwindow.h \
|
||||||
vdirectorytree.h \
|
vdirectorytree.h \
|
||||||
@ -81,7 +82,8 @@ HEADERS += vmainwindow.h \
|
|||||||
vdirectory.h \
|
vdirectory.h \
|
||||||
vfile.h \
|
vfile.h \
|
||||||
vnotebookselector.h \
|
vnotebookselector.h \
|
||||||
vnofocusitemdelegate.h
|
vnofocusitemdelegate.h \
|
||||||
|
vavatar.h
|
||||||
|
|
||||||
RESOURCES += \
|
RESOURCES += \
|
||||||
vnote.qrc
|
vnote.qrc
|
||||||
|
107
src/vavatar.cpp
Normal file
107
src/vavatar.cpp
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
#include "vavatar.h"
|
||||||
|
#include <QLabel>
|
||||||
|
#include <QPixmap>
|
||||||
|
#include <QHBoxLayout>
|
||||||
|
#include <QPainter>
|
||||||
|
#include <QBrush>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
VAvatar::VAvatar(QWidget *p_parent)
|
||||||
|
: QWidget(p_parent, Qt::FramelessWindowHint | Qt::WindowSystemMenuHint),
|
||||||
|
m_avatarText("VN"), m_diameter(48), m_borderWidth(3)
|
||||||
|
{
|
||||||
|
resize(m_diameter, m_diameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::paintEvent(QPaintEvent *p_event)
|
||||||
|
{
|
||||||
|
int diameter = width();
|
||||||
|
int x = diameter / 2;
|
||||||
|
int y = x + 1;
|
||||||
|
|
||||||
|
// Border
|
||||||
|
QPainterPath path;
|
||||||
|
path.addEllipse(QPoint(x, y), x - m_borderWidth, y - m_borderWidth);
|
||||||
|
|
||||||
|
QPainter painter(this);
|
||||||
|
painter.setClipPath(path);
|
||||||
|
painter.setClipping(true);
|
||||||
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
|
|
||||||
|
if (!m_avatarPixmap.isEmpty()) {
|
||||||
|
drawPixmap(painter);
|
||||||
|
} else {
|
||||||
|
drawText(painter, x);
|
||||||
|
}
|
||||||
|
drawBorder(painter, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::drawPixmap(QPainter &p_painter)
|
||||||
|
{
|
||||||
|
p_painter.drawPixmap(rect(), m_pixmap);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::drawText(QPainter &p_painter, int p_fontPixcel)
|
||||||
|
{
|
||||||
|
p_painter.save();
|
||||||
|
QFont font = p_painter.font();
|
||||||
|
font.setPixelSize(p_fontPixcel);
|
||||||
|
p_painter.setPen(m_fgColor);
|
||||||
|
p_painter.setFont(font);
|
||||||
|
p_painter.drawText(rect(), Qt::AlignCenter, m_avatarText);
|
||||||
|
p_painter.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::drawBorder(QPainter &p_painter, const QPainterPath &p_path)
|
||||||
|
{
|
||||||
|
p_painter.save();
|
||||||
|
p_painter.setClipping(false);
|
||||||
|
QPen borderPen(m_baseColor);
|
||||||
|
borderPen.setWidth(m_borderWidth);
|
||||||
|
p_painter.setPen(borderPen);
|
||||||
|
p_painter.drawPath(p_path);
|
||||||
|
p_painter.restore();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::setDiameter(int p_diameter)
|
||||||
|
{
|
||||||
|
if (m_diameter == p_diameter) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_diameter = p_diameter;
|
||||||
|
resize(m_diameter, m_diameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::setAvatarText(const QString &p_avatarText)
|
||||||
|
{
|
||||||
|
if (m_avatarText == p_avatarText) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_avatarText = p_avatarText.left(2);
|
||||||
|
m_avatarPixmap.clear();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::setAvatarPixmap(const QString &p_avatarPixmap)
|
||||||
|
{
|
||||||
|
if (m_avatarPixmap == p_avatarPixmap) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
m_avatarPixmap = p_avatarPixmap;
|
||||||
|
m_pixmap = QPixmap(m_avatarPixmap);
|
||||||
|
m_avatarText.clear();
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSize VAvatar::sizeHint() const
|
||||||
|
{
|
||||||
|
return QSize(m_diameter, m_diameter);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VAvatar::setColor(const QString &p_baseColor, const QString &p_fgColor, const QString &p_bgColor)
|
||||||
|
{
|
||||||
|
m_baseColor.setNamedColor(p_baseColor);
|
||||||
|
m_fgColor.setNamedColor(p_fgColor);
|
||||||
|
m_bgColor.setNamedColor(p_bgColor);
|
||||||
|
}
|
||||||
|
|
41
src/vavatar.h
Normal file
41
src/vavatar.h
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
#ifndef VAVATAR_H
|
||||||
|
#define VAVATAR_H
|
||||||
|
|
||||||
|
#include <QWidget>
|
||||||
|
#include <QString>
|
||||||
|
#include <QColor>
|
||||||
|
#include <QPixmap>
|
||||||
|
|
||||||
|
class QLabel;
|
||||||
|
|
||||||
|
class VAvatar : public QWidget
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit VAvatar(QWidget *p_parent = 0);
|
||||||
|
void setDiameter(int p_diameter);
|
||||||
|
void setAvatarPixmap(const QString &p_avatarPixmap);
|
||||||
|
void setAvatarText(const QString &p_avatarText);
|
||||||
|
void setColor(const QString &p_baseColor, const QString &p_fgColor, const QString &p_bgColor);
|
||||||
|
QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void drawPixmap(QPainter &p_painter);
|
||||||
|
void drawText(QPainter &p_painter, int p_fontPixcel);
|
||||||
|
void drawBorder(QPainter &p_painter, const QPainterPath &p_path);
|
||||||
|
|
||||||
|
// Draw a pixmap or characters.
|
||||||
|
QString m_avatarPixmap;
|
||||||
|
QString m_avatarText;
|
||||||
|
int m_diameter;
|
||||||
|
QColor m_baseColor;
|
||||||
|
QColor m_fgColor;
|
||||||
|
QColor m_bgColor;
|
||||||
|
int m_borderWidth;
|
||||||
|
QPixmap m_pixmap;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // VAVATAR_H
|
@ -9,6 +9,7 @@
|
|||||||
#include "veditarea.h"
|
#include "veditarea.h"
|
||||||
#include "voutline.h"
|
#include "voutline.h"
|
||||||
#include "vnotebookselector.h"
|
#include "vnotebookselector.h"
|
||||||
|
#include "vavatar.h"
|
||||||
|
|
||||||
extern VConfigManager vconfig;
|
extern VConfigManager vconfig;
|
||||||
|
|
||||||
@ -25,6 +26,7 @@ VMainWindow::VMainWindow(QWidget *parent)
|
|||||||
initToolBar();
|
initToolBar();
|
||||||
initMenuBar();
|
initMenuBar();
|
||||||
initDockWindows();
|
initDockWindows();
|
||||||
|
initAvatar();
|
||||||
restoreStateAndGeometry();
|
restoreStateAndGeometry();
|
||||||
|
|
||||||
notebookSelector->update();
|
notebookSelector->update();
|
||||||
@ -246,19 +248,19 @@ void VMainWindow::initActions()
|
|||||||
|
|
||||||
void VMainWindow::initToolBar()
|
void VMainWindow::initToolBar()
|
||||||
{
|
{
|
||||||
QToolBar *fileToolBar = addToolBar(tr("Note"));
|
m_fileToolBar = addToolBar(tr("Note"));
|
||||||
fileToolBar->setObjectName("NoteToolBar");
|
m_fileToolBar->setObjectName("NoteToolBar");
|
||||||
fileToolBar->setMovable(false);
|
m_fileToolBar->setMovable(false);
|
||||||
fileToolBar->addAction(newRootDirAct);
|
m_fileToolBar->addAction(newRootDirAct);
|
||||||
fileToolBar->addAction(newNoteAct);
|
m_fileToolBar->addAction(newNoteAct);
|
||||||
fileToolBar->addAction(noteInfoAct);
|
m_fileToolBar->addAction(noteInfoAct);
|
||||||
fileToolBar->addAction(deleteNoteAct);
|
m_fileToolBar->addAction(deleteNoteAct);
|
||||||
fileToolBar->addSeparator();
|
m_fileToolBar->addSeparator();
|
||||||
fileToolBar->addAction(editNoteAct);
|
m_fileToolBar->addAction(editNoteAct);
|
||||||
fileToolBar->addAction(saveExitAct);
|
m_fileToolBar->addAction(saveExitAct);
|
||||||
fileToolBar->addAction(discardExitAct);
|
m_fileToolBar->addAction(discardExitAct);
|
||||||
fileToolBar->addAction(saveNoteAct);
|
m_fileToolBar->addAction(saveNoteAct);
|
||||||
fileToolBar->addSeparator();
|
m_fileToolBar->addSeparator();
|
||||||
|
|
||||||
newRootDirAct->setEnabled(false);
|
newRootDirAct->setEnabled(false);
|
||||||
newNoteAct->setEnabled(false);
|
newNoteAct->setEnabled(false);
|
||||||
@ -269,12 +271,12 @@ void VMainWindow::initToolBar()
|
|||||||
discardExitAct->setVisible(false);
|
discardExitAct->setVisible(false);
|
||||||
saveNoteAct->setVisible(false);
|
saveNoteAct->setVisible(false);
|
||||||
|
|
||||||
QToolBar *viewToolBar = addToolBar(tr("View"));
|
m_viewToolBar = addToolBar(tr("View"));
|
||||||
viewToolBar->setObjectName("ViewToolBar");
|
m_viewToolBar->setObjectName("ViewToolBar");
|
||||||
viewToolBar->setMovable(false);
|
m_viewToolBar->setMovable(false);
|
||||||
viewToolBar->addAction(twoPanelViewAct);
|
m_viewToolBar->addAction(twoPanelViewAct);
|
||||||
viewToolBar->addAction(onePanelViewAct);
|
m_viewToolBar->addAction(onePanelViewAct);
|
||||||
viewToolBar->addAction(expandViewAct);
|
m_viewToolBar->addAction(expandViewAct);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VMainWindow::initMenuBar()
|
void VMainWindow::initMenuBar()
|
||||||
@ -352,6 +354,15 @@ void VMainWindow::initDockWindows()
|
|||||||
viewMenu->addAction(toolDock->toggleViewAction());
|
viewMenu->addAction(toolDock->toggleViewAction());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VMainWindow::initAvatar()
|
||||||
|
{
|
||||||
|
m_avatar = new VAvatar(this);
|
||||||
|
m_avatar->setAvatarPixmap(":/resources/icons/vnote.svg");
|
||||||
|
m_avatar->setColor(vnote->getColorFromPalette("base-color"), vnote->getColorFromPalette("Indigo4"),
|
||||||
|
vnote->getColorFromPalette("teal4"));
|
||||||
|
m_avatar->hide();
|
||||||
|
}
|
||||||
|
|
||||||
void VMainWindow::importNoteFromFile()
|
void VMainWindow::importNoteFromFile()
|
||||||
{
|
{
|
||||||
static QString lastPath = QDir::homePath();
|
static QString lastPath = QDir::homePath();
|
||||||
@ -633,7 +644,7 @@ void VMainWindow::restoreStateAndGeometry()
|
|||||||
|
|
||||||
const QVector<QPair<QString, QString> >& VMainWindow::getPalette() const
|
const QVector<QPair<QString, QString> >& VMainWindow::getPalette() const
|
||||||
{
|
{
|
||||||
return vnote->getPallete();
|
return vnote->getPalette();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VMainWindow::handleCurrentDirectoryChanged(const VDirectory *p_dir)
|
void VMainWindow::handleCurrentDirectoryChanged(const VDirectory *p_dir)
|
||||||
@ -645,3 +656,20 @@ void VMainWindow::handleCurrentNotebookChanged(const VNotebook *p_notebook)
|
|||||||
{
|
{
|
||||||
newRootDirAct->setEnabled(p_notebook);
|
newRootDirAct->setEnabled(p_notebook);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void VMainWindow::resizeEvent(QResizeEvent *event)
|
||||||
|
{
|
||||||
|
repositionAvatar();
|
||||||
|
QMainWindow::resizeEvent(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
void VMainWindow::repositionAvatar()
|
||||||
|
{
|
||||||
|
int diameter = mainSplitter->pos().y();
|
||||||
|
int x = width() - diameter - 5;
|
||||||
|
int y = 0;
|
||||||
|
qDebug() << "avatar:" << diameter << x << y;
|
||||||
|
m_avatar->setDiameter(diameter);
|
||||||
|
m_avatar->move(x, y);
|
||||||
|
m_avatar->show();
|
||||||
|
}
|
||||||
|
@ -25,6 +25,7 @@ class VEditArea;
|
|||||||
class QToolBox;
|
class QToolBox;
|
||||||
class VOutline;
|
class VOutline;
|
||||||
class VNotebookSelector;
|
class VNotebookSelector;
|
||||||
|
class VAvatar;
|
||||||
|
|
||||||
class VMainWindow : public QMainWindow
|
class VMainWindow : public QMainWindow
|
||||||
{
|
{
|
||||||
@ -51,6 +52,7 @@ private slots:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
|
void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setupUI();
|
void setupUI();
|
||||||
@ -59,6 +61,7 @@ private:
|
|||||||
void initToolBar();
|
void initToolBar();
|
||||||
void initMenuBar();
|
void initMenuBar();
|
||||||
void initDockWindows();
|
void initDockWindows();
|
||||||
|
void initAvatar();
|
||||||
void initPredefinedColorPixmaps();
|
void initPredefinedColorPixmaps();
|
||||||
void initRenderBackgroundMenu(QMenu *menu);
|
void initRenderBackgroundMenu(QMenu *menu);
|
||||||
void initEditorBackgroundMenu(QMenu *menu);
|
void initEditorBackgroundMenu(QMenu *menu);
|
||||||
@ -67,6 +70,7 @@ private:
|
|||||||
void updateToolbarFromTabChage(const VFile *p_file, bool p_editMode);
|
void updateToolbarFromTabChage(const VFile *p_file, bool p_editMode);
|
||||||
void saveStateAndGeometry();
|
void saveStateAndGeometry();
|
||||||
void restoreStateAndGeometry();
|
void restoreStateAndGeometry();
|
||||||
|
void repositionAvatar();
|
||||||
|
|
||||||
VNote *vnote;
|
VNote *vnote;
|
||||||
QPointer<VFile> m_curFile;
|
QPointer<VFile> m_curFile;
|
||||||
@ -81,6 +85,9 @@ private:
|
|||||||
QDockWidget *toolDock;
|
QDockWidget *toolDock;
|
||||||
QToolBox *toolBox;
|
QToolBox *toolBox;
|
||||||
VOutline *outline;
|
VOutline *outline;
|
||||||
|
VAvatar *m_avatar;
|
||||||
|
QToolBar *m_fileToolBar;
|
||||||
|
QToolBar *m_viewToolBar;
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
QAction *newRootDirAct;
|
QAction *newRootDirAct;
|
||||||
|
@ -46,6 +46,16 @@ void VNote::initPalette(QPalette palette)
|
|||||||
m_palette.append(QPair<QString, QString>("Indigo4", "#5C6BC0"));
|
m_palette.append(QPair<QString, QString>("Indigo4", "#5C6BC0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString VNote::getColorFromPalette(const QString &p_name) const
|
||||||
|
{
|
||||||
|
for (int i = 0; i < m_palette.size(); ++i) {
|
||||||
|
if (m_palette[i].first == p_name) {
|
||||||
|
return m_palette[i].second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "White";
|
||||||
|
}
|
||||||
|
|
||||||
void VNote::initTemplate()
|
void VNote::initTemplate()
|
||||||
{
|
{
|
||||||
if (templateHtml.isEmpty() || preTemplateHtml.isEmpty()
|
if (templateHtml.isEmpty() || preTemplateHtml.isEmpty()
|
||||||
|
@ -30,8 +30,9 @@ public:
|
|||||||
static QString preTemplateHtml;
|
static QString preTemplateHtml;
|
||||||
static QString postTemplateHtml;
|
static QString postTemplateHtml;
|
||||||
|
|
||||||
inline const QVector<QPair<QString, QString> > &getPallete() const;
|
inline const QVector<QPair<QString, QString> > &getPalette() const;
|
||||||
void initPalette(QPalette palette);
|
void initPalette(QPalette palette);
|
||||||
|
QString getColorFromPalette(const QString &p_name) const;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void updateTemplate();
|
void updateTemplate();
|
||||||
@ -42,7 +43,7 @@ private:
|
|||||||
QVector<QPair<QString, QString> > m_palette;
|
QVector<QPair<QString, QString> > m_palette;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline const QVector<QPair<QString, QString> >& VNote::getPallete() const
|
inline const QVector<QPair<QString, QString> >& VNote::getPalette() const
|
||||||
{
|
{
|
||||||
return m_palette;
|
return m_palette;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user