From 2ce96a05bc94d87a415e2fc161c68dd8dda99fbc Mon Sep 17 00:00:00 2001 From: fourtf Date: Fri, 30 Dec 2016 12:20:26 +0100 Subject: [PATCH] added basic tabs --- chatterino.pro | 6 ++-- mainwindow.cpp | 12 ++++++-- mainwindow.h | 2 ++ notebook.cpp | 61 ++++++++++++++++++++++++++++++++------ notebook.h | 16 ++++++++-- notebookbutton.cpp | 74 +++++++++++++++++++++++++++++++++++++++++++++- notebookbutton.h | 24 +++++++++++++-- notebookpage.cpp | 5 ++-- notebookpage.h | 5 +++- notebooktab.cpp | 45 ++++++++++++++++++++++++++-- notebooktab.h | 17 +++++++++-- 11 files changed, 240 insertions(+), 27 deletions(-) diff --git a/chatterino.pro b/chatterino.pro index 0d6385c4d..c24d51291 100644 --- a/chatterino.pro +++ b/chatterino.pro @@ -28,12 +28,14 @@ SOURCES += main.cpp\ chatwidget.cpp \ notebook.cpp \ notebooktab.cpp \ - notebookpage.cpp + notebookpage.cpp \ + notebookbutton.cpp HEADERS += mainwindow.h \ chatwidget.h \ notebook.h \ notebooktab.h \ - notebookpage.h + notebookpage.h \ + notebookbutton.h FORMS += mainwindow.ui diff --git a/mainwindow.cpp b/mainwindow.cpp index 3f72c87b4..706797854 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,16 +1,24 @@ #include "mainwindow.h" #include "ui_mainwindow.h" #include "chatwidget.h" +#include "notebook.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + notebook(this) { //ui->setupUi(this); //ChatWidget widget = new ChatWidget(this); - setCentralWidget(new ChatWidget(this)); + //this->notebook = new Notebook(this); + + setCentralWidget(&this->notebook); + + this->notebook.addPage(); + this->notebook.addPage(); + this->notebook.addPage(); } MainWindow::~MainWindow() diff --git a/mainwindow.h b/mainwindow.h index a3948a917..3ad87afd3 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include "notebook.h" namespace Ui { class MainWindow; @@ -14,6 +15,7 @@ class MainWindow : public QMainWindow public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + Notebook notebook; private: Ui::MainWindow *ui; diff --git a/notebook.cpp b/notebook.cpp index cdcc87bbd..078be4879 100644 --- a/notebook.cpp +++ b/notebook.cpp @@ -1,22 +1,65 @@ #include "QWidget" +#include "QList" #include "notebook.h" +#include "notebooktab.h" #include "notebookpage.h" +#include "notebookbutton.h" Notebook::Notebook(QWidget *parent) - : QWidget(parent) + : QWidget(parent), + addButton(this), + settingsButton(this), + userButton(this) { - auto list = new QList>(); - - this->pages = list; + settingsButton.resize(24, 24); + settingsButton.icon = NotebookButton::IconSettings; + userButton.resize(24, 24); + userButton.move(24, 0); + userButton.icon = NotebookButton::IconUser; + addButton.resize(24, 24); } -NotebookPage* -Notebook::AddPage() +NotebookPage* Notebook::addPage() { - auto page = new NotebookPage(this); - auto tab = new NotebookTab(this, this, page); + auto tab = new NotebookTab(this); + auto page = new NotebookPage(this, tab); - this->pages->append(std::make_tuple(page, tab)); + this->pages.append(page); + + layout(); return page; } + +void Notebook::layout() +{ + int x = 48, y = 0; + int tabHeight = 16; + bool firstInLine = true; + + for (auto &i : this->pages) + { + tabHeight = i->tab->height(); + + if (!firstInLine && (i == this->pages.last() ? tabHeight : 0) + x + i->width() > this->width()) + { + y +=i->tab->height(); + i->tab->move(0, y); + x = i->tab->width(); + } + else + { + i->tab->move(x, y); + x += i->tab->width(); + } + + firstInLine = false; + } + + this->addButton.move(x, y); +} + +void Notebook::resizeEvent(QResizeEvent *) +{ + layout(); +} diff --git a/notebook.h b/notebook.h index f84003776..13a7301bf 100644 --- a/notebook.h +++ b/notebook.h @@ -5,6 +5,7 @@ #include #include "notebookpage.h" #include "notebooktab.h" +#include "notebookbutton.h" class Notebook : public QWidget { @@ -13,10 +14,21 @@ Q_OBJECT public: Notebook(QWidget *parent); - NotebookPage* AddPage(); + NotebookPage* addPage(); + + enum HighlightType { none, highlighted, newMessage }; + +protected: + void resizeEvent(QResizeEvent *); private: - QList>* pages; + QList pages; + + NotebookButton addButton; + NotebookButton settingsButton; + NotebookButton userButton; + + void layout(); }; #endif // NOTEBOOK_H diff --git a/notebookbutton.cpp b/notebookbutton.cpp index 302bffb23..687fa4042 100644 --- a/notebookbutton.cpp +++ b/notebookbutton.cpp @@ -1,6 +1,78 @@ #include "notebookbutton.h" +#include "QPainter" -NotebookButton::NotebookButton() +NotebookButton::NotebookButton(QWidget *parent) + : QWidget(parent) { } + +void NotebookButton::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + + QColor background; + QColor foreground; + + if (mouseDown) + { + foreground = QColor(0, 0, 0); + background = QColor(255, 255, 255); + } + else if (mouseOver) + { + foreground = QColor(255, 255, 255); + background = QColor(0, 0, 0); + } + else + { + foreground = QColor(0, 0, 0); + background = QColor(255, 255, 255); + } + + painter.fillRect(this->rect(), background); + + float h = this->height(), w = this->width(); + + if (icon == IconPlus) + { + painter.fillRect(QRectF((h / 12) * 2 + 1, (h / 12) * 5 + 1, w - ((h / 12) * 5), (h / 12) * 1), foreground); + painter.fillRect(QRectF((h / 12) * 5 + 1, (h / 12) * 2 + 1, (h / 12) * 1, w - ((h / 12) * 5)), foreground); + } + else if (icon == IconUser) + { + + } + else // IconSettings + { + + } +} + +void NotebookButton::mousePressEvent(QMouseEvent *) +{ + mouseDown = true; + + this->repaint(); +} + +void NotebookButton::mouseReleaseEvent(QMouseEvent *) +{ + mouseDown = false; + + this->repaint(); +} + +void NotebookButton::enterEvent(QEvent *) +{ + mouseOver = true; + + this->repaint(); +} + +void NotebookButton::leaveEvent(QEvent *) +{ + mouseOver = false; + + this->repaint(); +} diff --git a/notebookbutton.h b/notebookbutton.h index 83856fb71..44b5047f9 100644 --- a/notebookbutton.h +++ b/notebookbutton.h @@ -1,11 +1,29 @@ #ifndef NOTEBOOKBUTTON_H #define NOTEBOOKBUTTON_H +#include -class NotebookButton +class NotebookButton : public QWidget { + Q_OBJECT public: - NotebookButton(); + static const int IconPlus = 0; + static const int IconUser = 0; + static const int IconSettings = 0; + + int icon = 0; + + NotebookButton(QWidget *parent); + + void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; + void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE; + void enterEvent(QEvent *) Q_DECL_OVERRIDE; + void leaveEvent(QEvent *) Q_DECL_OVERRIDE; + +private: + bool mouseOver = false; + bool mouseDown = false; }; -#endif // NOTEBOOKBUTTON_H \ No newline at end of file +#endif // NOTEBOOKBUTTON_H diff --git a/notebookpage.cpp b/notebookpage.cpp index 260967da1..21281f501 100644 --- a/notebookpage.cpp +++ b/notebookpage.cpp @@ -1,8 +1,9 @@ #include "QWidget" #include "notebookpage.h" +#include "notebooktab.h" -NotebookPage::NotebookPage(QWidget *parent) +NotebookPage::NotebookPage(QWidget *parent, NotebookTab *tab) : QWidget(parent) { - + this->tab = tab; } diff --git a/notebookpage.h b/notebookpage.h index cc0d8b46f..02b328071 100644 --- a/notebookpage.h +++ b/notebookpage.h @@ -3,12 +3,15 @@ #include "QWidget" +class NotebookTab; + class NotebookPage : public QWidget { Q_OBJECT public: - NotebookPage(QWidget *parent); + NotebookPage(QWidget *parent, NotebookTab *tab); + NotebookTab* tab; }; #endif // NOTEBOOKPAGE_H diff --git a/notebooktab.cpp b/notebooktab.cpp index 97013ed38..f296f8228 100644 --- a/notebooktab.cpp +++ b/notebooktab.cpp @@ -1,10 +1,49 @@ #include "notebook.h" #include "notebooktab.h" #include "notebookpage.h" +#include "QPainter" -NotebookTab::NotebookTab(QWidget *parent, Notebook *notebook, NotebookPage *page) - : QWidget(parent) +NotebookTab::NotebookTab(Notebook *notebook) + : QWidget(notebook) { this->notebook = notebook; - this->page = page; + + resize(100, 24); +} + +void NotebookTab::paintEvent(QPaintEvent *) +{ + QPainter painter(this); + + painter.setBrush(this->mouseDown ? QColor(0, 255, 0) : (this->mouseOver ? QColor(255, 0, 0) : QColor(0, 0, 255))); + + painter.drawRect(0, 0, this->width(), this->height()); +} + +void NotebookTab::mousePressEvent(QMouseEvent *) +{ + mouseDown = true; + + this->repaint(); +} + +void NotebookTab::mouseReleaseEvent(QMouseEvent *) +{ + mouseDown = false; + + this->repaint(); +} + +void NotebookTab::enterEvent(QEvent *) +{ + mouseOver = true; + + this->repaint(); +} + +void NotebookTab::leaveEvent(QEvent *) +{ + mouseOver = false; + + this->repaint(); } diff --git a/notebooktab.h b/notebooktab.h index 617e1f1fa..e1a67cafe 100644 --- a/notebooktab.h +++ b/notebooktab.h @@ -11,11 +11,24 @@ class NotebookTab : public QWidget Q_OBJECT public: - NotebookTab(QWidget *parent, Notebook *notebook, NotebookPage *page); + NotebookTab(Notebook *notebook); + +protected: + void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE; + + void mousePressEvent(QMouseEvent *) Q_DECL_OVERRIDE; + void mouseReleaseEvent(QMouseEvent *) Q_DECL_OVERRIDE; + void enterEvent(QEvent *) Q_DECL_OVERRIDE; + void leaveEvent(QEvent *) Q_DECL_OVERRIDE; + + bool IsSelected(); private: Notebook *notebook; - NotebookPage *page; + + bool isSelected; + bool mouseOver = false; + bool mouseDown = false; }; #endif // NOTEBOOKTAB_H