mirror-chatterino2/src/widgets/window.cpp

153 lines
3.8 KiB
C++
Raw Normal View History

2017-11-12 17:21:50 +01:00
#include "widgets/window.hpp"
#include "accountmanager.hpp"
#include "channelmanager.hpp"
2017-06-11 09:31:45 +02:00
#include "colorscheme.hpp"
#include "settingsmanager.hpp"
#include "widgets/notebook.hpp"
#include "widgets/settingsdialog.hpp"
2017-12-17 02:18:13 +01:00
#include "widgets/split.hpp"
2016-12-29 17:31:07 +01:00
2017-01-18 04:52:47 +01:00
#include <QPalette>
#include <QShortcut>
2017-04-12 17:46:44 +02:00
#include <QVBoxLayout>
2017-01-18 04:52:47 +01:00
2017-04-14 17:52:22 +02:00
namespace chatterino {
namespace widgets {
2017-01-18 21:30:23 +01:00
Window::Window(const QString &windowName, ChannelManager &_channelManager,
ColorScheme &_colorScheme, bool _isMainWindow)
: BaseWidget(_colorScheme, nullptr)
, settingRoot(fS("/windows/{}", windowName))
, windowGeometry(this->settingRoot)
, dpi(this->getDpiMultiplier())
, channelManager(_channelManager)
, colorScheme(_colorScheme)
, notebook(this->channelManager, this, _isMainWindow, this->settingRoot)
2016-12-29 17:31:07 +01:00
{
this->initAsWindow();
AccountManager::getInstance().Twitch.currentUsername.connect(
[this](const std::string &newUsername, auto) {
if (newUsername.empty()) {
this->refreshWindowTitle("Not logged in");
} else {
this->refreshWindowTitle(QString::fromStdString(newUsername));
}
});
2017-04-12 17:46:44 +02:00
QVBoxLayout *layout = new QVBoxLayout(this);
// add titlebar
// if (SettingsManager::getInstance().useCustomWindowFrame.get()) {
// layout->addWidget(&_titleBar);
// }
layout->addWidget(&this->notebook);
2017-04-12 17:46:44 +02:00
setLayout(layout);
// set margin
// if (SettingsManager::getInstance().useCustomWindowFrame.get()) {
// layout->setMargin(1);
// } else {
layout->setMargin(0);
// }
2016-12-30 12:20:26 +01:00
this->refreshTheme();
2017-01-01 02:30:42 +01:00
this->loadGeometry();
// Initialize program-wide hotkeys
{
// CTRL+P: Open Settings Dialog
auto shortcut = new QShortcut(QKeySequence("CTRL+P"), this);
connect(shortcut, &QShortcut::activated, []() {
SettingsDialog::showDialog(); //
});
}
2016-12-29 17:31:07 +01:00
}
2017-11-12 17:21:50 +01:00
void Window::repaintVisibleChatWidgets(Channel *channel)
{
auto *page = this->notebook.getSelectedPage();
if (page == nullptr) {
return;
}
2017-11-12 17:21:50 +01:00
const std::vector<Split *> &widgets = page->getChatWidgets();
for (auto it = widgets.begin(); it != widgets.end(); ++it) {
2017-11-12 17:21:50 +01:00
Split *widget = *it;
if (channel == nullptr || channel == widget->getChannel().get()) {
2017-04-12 17:46:44 +02:00
widget->layoutMessages();
}
}
}
2017-11-12 17:21:50 +01:00
Notebook &Window::getNotebook()
2017-04-12 17:46:44 +02:00
{
return this->notebook;
2017-04-12 17:46:44 +02:00
}
void Window::refreshWindowTitle(const QString &username)
{
this->setWindowTitle(username + " - Chatterino for Twitch");
}
2017-11-12 17:21:50 +01:00
void Window::closeEvent(QCloseEvent *)
{
const QRect &geom = this->geometry();
this->windowGeometry.x = geom.x();
this->windowGeometry.y = geom.y();
this->windowGeometry.width = geom.width();
this->windowGeometry.height = geom.height();
2017-12-14 00:25:06 +01:00
this->closed();
}
void Window::changeEvent(QEvent *event)
{
if(!this->isActiveWindow())
{
this->lostFocus.invoke();
}
BaseWidget::changeEvent(event);
}
void Window::leaveEvent(QEvent *event)
{
this->lostFocus.invoke();
BaseWidget::leaveEvent(event);
}
2017-11-12 17:21:50 +01:00
void Window::refreshTheme()
{
QPalette palette;
2017-08-17 14:52:41 +02:00
palette.setColor(QPalette::Background, this->colorScheme.TabBackground);
this->setPalette(palette);
}
void Window::loadGeometry()
{
if (!this->windowGeometry.x.isDefaultValue() && !this->windowGeometry.y.isDefaultValue()) {
this->move(this->windowGeometry.x, this->windowGeometry.y);
}
if (!this->windowGeometry.width.isDefaultValue() &&
!this->windowGeometry.height.isDefaultValue()) {
this->resize(this->windowGeometry.width, this->windowGeometry.height);
} else {
this->resize(1280, 800);
}
}
void Window::save()
{
this->notebook.save();
}
2017-04-14 17:52:22 +02:00
} // namespace widgets
} // namespace chatterino