added basic tabs

This commit is contained in:
fourtf 2016-12-30 12:20:26 +01:00
parent cfda4798d4
commit 2ce96a05bc
11 changed files with 240 additions and 27 deletions

View file

@ -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

View file

@ -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()

View file

@ -2,6 +2,7 @@
#define MAINWINDOW_H
#include <QMainWindow>
#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;

View file

@ -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<std::tuple<NotebookPage*, NotebookTab*>>();
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();
}

View file

@ -5,6 +5,7 @@
#include <QList>
#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<std::tuple<NotebookPage*, NotebookTab*>>* pages;
QList<NotebookPage*> pages;
NotebookButton addButton;
NotebookButton settingsButton;
NotebookButton userButton;
void layout();
};
#endif // NOTEBOOK_H

View file

@ -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();
}

View file

@ -1,11 +1,29 @@
#ifndef NOTEBOOKBUTTON_H
#define NOTEBOOKBUTTON_H
#include <QWidget>
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
#endif // NOTEBOOKBUTTON_H

View file

@ -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;
}

View file

@ -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

View file

@ -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();
}

View file

@ -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