mirror-chatterino2/notebookpage.cpp

232 lines
5.7 KiB
C++
Raw Normal View History

2017-01-11 18:52:09 +01:00
#include "notebookpage.h"
2017-01-01 02:30:42 +01:00
#include "QHBoxLayout"
#include "QMimeData"
2017-01-11 18:52:09 +01:00
#include "QPainter"
#include "QVBoxLayout"
#include "QWidget"
2017-01-01 02:30:42 +01:00
#include "chatwidget.h"
2017-01-11 18:52:09 +01:00
#include "colorscheme.h"
#include "notebooktab.h"
2016-12-29 17:31:07 +01:00
2017-01-01 02:30:42 +01:00
bool NotebookPage::isDraggingSplit = false;
2017-01-11 18:52:09 +01:00
ChatWidget *NotebookPage::draggingSplit = NULL;
2017-01-01 02:30:42 +01:00
std::pair<int, int> NotebookPage::dropPosition = std::pair<int, int>(-1, -1);
NotebookPage::NotebookPage(QWidget *parent, NotebookTab *tab)
2017-01-11 18:52:09 +01:00
: QWidget(parent)
, m_parentbox(this)
, m_preview(this)
, m_chatWidgets()
2016-12-29 17:31:07 +01:00
{
2016-12-30 12:20:26 +01:00
this->tab = tab;
2016-12-30 18:00:25 +01:00
tab->page = this;
2017-01-01 02:30:42 +01:00
setHidden(true);
setAcceptDrops(true);
m_parentbox.addSpacing(2);
m_parentbox.addLayout(&m_hbox);
m_parentbox.setMargin(0);
2017-01-01 02:30:42 +01:00
m_hbox.setSpacing(1);
m_hbox.setMargin(0);
2017-01-01 02:30:42 +01:00
}
2017-01-11 18:52:09 +01:00
std::pair<int, int>
NotebookPage::removeFromLayout(ChatWidget *widget)
2017-01-01 02:30:42 +01:00
{
for (auto it = m_chatWidgets.begin(); it != m_chatWidgets.end(); ++it) {
if (*it == widget) {
m_chatWidgets.erase(it);
break;
}
}
for (int i = 0; i < m_hbox.count(); ++i) {
auto vbox = static_cast<QVBoxLayout *>(m_hbox.itemAt(i));
2017-01-01 02:30:42 +01:00
2017-01-11 18:52:09 +01:00
for (int j = 0; j < vbox->count(); ++j) {
if (vbox->itemAt(j)->widget() != widget)
continue;
2017-01-01 02:30:42 +01:00
widget->setParent(NULL);
bool isLastItem = vbox->count() == 0;
2017-01-11 18:52:09 +01:00
if (isLastItem) {
m_hbox.removeItem(vbox);
2017-01-01 02:30:42 +01:00
delete vbox;
}
2017-01-11 18:52:09 +01:00
return std::pair<int, int>(i, isLastItem ? -1 : j);
2017-01-01 02:30:42 +01:00
}
}
return std::pair<int, int>(-1, -1);
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::addToLayout(
ChatWidget *widget,
std::pair<int, int> position = std::pair<int, int>(-1, -1))
2017-01-01 02:30:42 +01:00
{
m_chatWidgets.push_back(widget);
2017-01-01 02:30:42 +01:00
// add vbox at the end
if (position.first < 0 || position.first >= m_hbox.count()) {
2017-01-01 02:30:42 +01:00
auto vbox = new QVBoxLayout();
vbox->addWidget(widget);
m_hbox.addLayout(vbox);
2017-01-01 02:30:42 +01:00
return;
}
// insert vbox
2017-01-11 18:52:09 +01:00
if (position.second == -1) {
2017-01-01 02:30:42 +01:00
auto vbox = new QVBoxLayout();
vbox->addWidget(widget);
m_hbox.insertLayout(position.first, vbox);
2017-01-01 02:30:42 +01:00
return;
}
// add to existing vbox
auto vbox = static_cast<QVBoxLayout *>(m_hbox.itemAt(position.first));
2017-01-01 02:30:42 +01:00
2017-01-11 18:52:09 +01:00
vbox->insertWidget(std::max(0, std::min(vbox->count(), position.second)),
widget);
2017-01-01 02:30:42 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::enterEvent(QEvent *)
{
if (m_hbox.count() == 0) {
setCursor(QCursor(Qt::PointingHandCursor));
2017-01-11 18:52:09 +01:00
} else {
setCursor(QCursor(Qt::ArrowCursor));
}
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::leaveEvent(QEvent *)
{
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::mouseReleaseEvent(QMouseEvent *event)
{
if (m_hbox.count() == 0 && event->button() == Qt::LeftButton) {
addToLayout(new ChatWidget(), std::pair<int, int>(-1, -1));
setCursor(QCursor(Qt::ArrowCursor));
}
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::dragEnterEvent(QDragEnterEvent *event)
2017-01-01 02:30:42 +01:00
{
2017-01-11 18:52:09 +01:00
if (!event->mimeData()->hasFormat("chatterino/split"))
return;
2017-01-01 02:30:42 +01:00
2017-01-11 18:52:09 +01:00
if (isDraggingSplit) {
m_dropRegions.clear();
2017-01-01 02:30:42 +01:00
if (m_hbox.count() == 0) {
m_dropRegions.push_back(
2017-01-11 18:52:09 +01:00
DropRegion(rect(), std::pair<int, int>(-1, -1)));
} else {
for (int i = 0; i < m_hbox.count() + 1; ++i) {
m_dropRegions.push_back(DropRegion(
QRect(((i * 4 - 1) * width() / m_hbox.count()) / 4, 0,
width() / m_hbox.count() / 2, height()),
std::pair<int, int>(i, -1)));
}
2017-01-01 02:30:42 +01:00
for (int i = 0; i < m_hbox.count(); ++i) {
auto vbox = static_cast<QVBoxLayout *>(m_hbox.itemAt(i));
2017-01-11 18:52:09 +01:00
for (int j = 0; j < vbox->count() + 1; ++j) {
m_dropRegions.push_back(DropRegion(
QRect(i * width() / m_hbox.count(),
2017-01-11 18:52:09 +01:00
((j * 2 - 1) * height() / vbox->count()) / 2,
width() / m_hbox.count(),
height() / vbox->count()),
2017-01-11 18:52:09 +01:00
std::pair<int, int>(i, j)));
}
2017-01-01 02:30:42 +01:00
}
}
setPreviewRect(event->pos());
event->acceptProposedAction();
}
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::dragMoveEvent(QDragMoveEvent *event)
2017-01-01 02:30:42 +01:00
{
setPreviewRect(event->pos());
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::setPreviewRect(QPoint mousePos)
2017-01-01 02:30:42 +01:00
{
for (DropRegion region : m_dropRegions) {
2017-01-11 18:52:09 +01:00
if (region.rect.contains(mousePos)) {
m_preview.move(region.rect.x(), region.rect.y());
m_preview.resize(region.rect.width(), region.rect.height());
m_preview.show();
m_preview.raise();
2017-01-01 02:30:42 +01:00
dropPosition = region.position;
return;
2017-01-11 18:52:09 +01:00
} else {
m_preview.hide();
2017-01-01 02:30:42 +01:00
}
}
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::dragLeaveEvent(QDragLeaveEvent *event)
2017-01-01 02:30:42 +01:00
{
m_preview.hide();
2017-01-01 02:30:42 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::dropEvent(QDropEvent *event)
2017-01-01 02:30:42 +01:00
{
2017-01-11 18:52:09 +01:00
if (isDraggingSplit) {
2017-01-01 02:30:42 +01:00
event->acceptProposedAction();
NotebookPage::draggingSplit->setParent(this);
addToLayout(NotebookPage::draggingSplit, dropPosition);
}
m_preview.hide();
2016-12-30 18:00:25 +01:00
}
2017-01-11 18:52:09 +01:00
void
NotebookPage::paintEvent(QPaintEvent *)
2016-12-30 18:00:25 +01:00
{
QPainter painter(this);
if (m_hbox.count() == 0) {
2017-01-06 23:28:48 +01:00
painter.fillRect(rect(), ColorScheme::instance().ChatBackground);
2017-01-01 02:30:42 +01:00
2017-01-11 18:52:09 +01:00
painter.fillRect(0, 0, width(), 2,
ColorScheme::instance().TabSelectedBackground);
2017-01-01 02:30:42 +01:00
2017-01-06 23:28:48 +01:00
painter.setPen(ColorScheme::instance().Text);
painter.drawText(rect(), "Add Chat", QTextOption(Qt::AlignCenter));
2017-01-11 18:52:09 +01:00
} else {
2017-01-06 23:28:48 +01:00
painter.fillRect(rect(), ColorScheme::instance().TabSelectedBackground);
2016-12-30 18:00:25 +01:00
2017-01-11 18:52:09 +01:00
painter.fillRect(0, 0, width(), 2,
ColorScheme::instance().TabSelectedBackground);
}
2016-12-29 17:31:07 +01:00
}