added tab system

This commit is contained in:
fourtf 2016-12-30 18:00:25 +01:00
parent 42b5de161d
commit bb91ec2045
6 changed files with 128 additions and 28 deletions

View file

@ -1,9 +1,11 @@
#include "QWidget"
#include "QList"
#include "QLayout"
#include "notebook.h"
#include "notebooktab.h"
#include "notebookpage.h"
#include "notebookbutton.h"
#include "QFormLayout"
Notebook::Notebook(QWidget *parent)
: QWidget(parent),
@ -22,26 +24,48 @@ Notebook::Notebook(QWidget *parent)
NotebookPage* Notebook::addPage()
{
auto tab = new NotebookTab(this);
auto page = new NotebookPage(this, tab);
auto page = new NotebookPage(tab);
if (pages.count() == 0)
{
select(page);
}
this->pages.append(page);
layout();
return page;
}
void Notebook::layout()
void Notebook::select(NotebookPage* page)
{
if (selected != nullptr)
{
selected->setParent(nullptr);
selected->tab->setSelected(false);
}
if (page != nullptr)
{
page->setParent(this);
page->tab->setSelected(true);
}
selected = page;
performLayout();
}
void Notebook::performLayout()
{
int x = 48, y = 0;
int tabHeight = 16;
bool firstInLine = true;
bool first = true;
for (auto &i : this->pages)
for (auto &i : pages)
{
tabHeight = i->tab->height();
if (!firstInLine && (i == this->pages.last() ? tabHeight : 0) + x + i->width() > this->width())
if (!first && (i == pages.last() ? tabHeight : 0) + x + i->tab->width() > width())
{
y +=i->tab->height();
i->tab->move(0, y);
@ -53,13 +77,19 @@ void Notebook::layout()
x += i->tab->width();
}
firstInLine = false;
first = false;
}
this->addButton.move(x, y);
if (selected != nullptr)
{
selected->move(0, y + tabHeight);
selected->resize(width(), height() - y - tabHeight);
}
}
void Notebook::resizeEvent(QResizeEvent *)
{
layout();
performLayout();
}

View file

@ -18,6 +18,9 @@ public:
enum HighlightType { none, highlighted, newMessage };
void select(NotebookPage* page);
void performLayout();
protected:
void resizeEvent(QResizeEvent *);
@ -28,7 +31,7 @@ private:
NotebookButton settingsButton;
NotebookButton userButton;
void layout();
NotebookPage* selected = nullptr;
};
#endif // NOTEBOOK_H

View file

@ -1,9 +1,20 @@
#include "QWidget"
#include "QPainter"
#include "notebookpage.h"
#include "notebooktab.h"
NotebookPage::NotebookPage(QWidget *parent, NotebookTab *tab)
: QWidget(parent)
NotebookPage::NotebookPage(NotebookTab *tab)
{
this->tab = tab;
tab->page = this;
}
void NotebookPage::paintEvent(QPaintEvent *)
{
QPainter painter(this);
painter.setPen(QColor(255, 0, 0));
painter.drawRect(0, 0, width() - 1, height() - 1);
painter.drawText(8, 8, QString::number(tab->x()));
}

View file

@ -2,16 +2,19 @@
#define NOTEBOOKPAGE_H
#include "QWidget"
class NotebookTab;
#include "notebookpage.h"
#include "notebooktab.h"
class NotebookPage : public QWidget
{
Q_OBJECT
public:
NotebookPage(QWidget *parent, NotebookTab *tab);
NotebookPage(NotebookTab *tab);
NotebookTab* tab;
protected:
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
};
#endif // NOTEBOOKPAGE_H

View file

@ -1,49 +1,96 @@
#include "notebook.h"
#include "notebooktab.h"
#include "notebookpage.h"
#include "QPainter"
NotebookTab::NotebookTab(Notebook *notebook)
: QWidget(notebook)
{
this->notebook = notebook;
text = "<no title>";
resize(100, 24);
calcSize();
}
void NotebookTab::setSelected(bool value)
{
selected = value;
repaint();
}
bool NotebookTab::getSelected()
{
return selected;
}
void NotebookTab::calcSize()
{
resize(fontMetrics().width(text) + 8, 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)));
QColor bg = QColor(255, 255, 255), fg = QColor(0, 0, 0);
painter.drawRect(0, 0, this->width(), this->height());
// if (selected)
// {
// bg = App.ColorScheme.TabSelectedBG;
// text = App.ColorScheme.TabSelectedText;
// }
// else if (mouseOver)
// {
// bg = App.ColorScheme.TabHoverBG;
// text = App.ColorScheme.TabHoverText;
// }
// else if ()
// {
// bg = App.ColorScheme.TabHighlightedBG;
// text = App.ColorScheme.TabHighlightedText;
// }
// else if ()
// {
// bg = App.ColorScheme.TabNewMessageBG;
// text = App.ColorScheme.TabHighlightedText;
// }
// else
// {
// bg = App.ColorScheme.TabBG;
// text = App.ColorScheme.TabText;
// }
painter.fillRect(rect(), bg);
painter.setPen(fg);
painter.drawText(4, (height() + fontMetrics().height()) / 2, text);
}
void NotebookTab::mousePressEvent(QMouseEvent *)
{
mouseDown = true;
this->repaint();
repaint();
}
void NotebookTab::mouseReleaseEvent(QMouseEvent *)
{
mouseDown = false;
this->repaint();
repaint();
notebook->select(page);
}
void NotebookTab::enterEvent(QEvent *)
{
mouseOver = true;
this->repaint();
repaint();
}
void NotebookTab::leaveEvent(QEvent *)
{
mouseOver = false;
this->repaint();
repaint();
}

View file

@ -1,10 +1,10 @@
#ifndef NOTEBOOKTAB_H
#define NOTEBOOKTAB_H
#include "QObject"
#include "notebookpage.h"
#include "QWidget"
class Notebook;
class NotebookPage;
class NotebookTab : public QWidget
{
@ -13,6 +13,14 @@ Q_OBJECT
public:
NotebookTab(Notebook *notebook);
void calcSize();
NotebookPage* page;
QString text;
bool getSelected();
void setSelected(bool value);
protected:
void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
@ -21,12 +29,10 @@ protected:
void enterEvent(QEvent *) Q_DECL_OVERRIDE;
void leaveEvent(QEvent *) Q_DECL_OVERRIDE;
bool IsSelected();
private:
Notebook *notebook;
bool selected = false;
bool isSelected;
bool mouseOver = false;
bool mouseDown = false;
};