Implement console window tray icon, console window hiding, better scrolling

This commit is contained in:
Petr Mrázek 2014-01-06 02:52:51 +01:00
parent ddb961fad5
commit fcb8612c10
2 changed files with 96 additions and 27 deletions

View File

@ -19,12 +19,14 @@
#include <QScrollBar>
#include <QMessageBox>
#include <QSystemTrayIcon>
#include <gui/Platform.h>
#include <gui/dialogs/CustomMessageBox.h>
#include <gui/dialogs/ProgressDialog.h>
#include "logic/net/PasteUpload.h"
#include "logic/icons/IconList.h"
ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent)
: QMainWindow(parent), ui(new Ui::ConsoleWindow), proc(mcproc)
@ -40,9 +42,23 @@ ConsoleWindow::ConsoleWindow(MinecraftProcess *mcproc, QWidget *parent)
connect(mcproc, SIGNAL(launch_failed(BaseInstance *)), this,
SLOT(onLaunchFailed(BaseInstance *)));
restoreState(QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowState").toByteArray()));
restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowGeometry").toByteArray()));
restoreState(
QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowState").toByteArray()));
restoreGeometry(
QByteArray::fromBase64(MMC->settings()->get("ConsoleWindowGeometry").toByteArray()));
QString iconKey = proc->instance()->iconKey();
QString name = proc->instance()->name();
auto icon = MMC->icons()->getIcon(iconKey);
setWindowIcon(icon);
m_trayIcon = new QSystemTrayIcon(icon, this);
QString consoleTitle = tr("Console window for ") + name;
m_trayIcon->setToolTip(consoleTitle);
setWindowTitle(consoleTitle);
connect(m_trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
m_trayIcon->show();
if (mcproc->instance()->settings().get("ShowConsole").toBool())
{
show();
@ -55,6 +71,19 @@ ConsoleWindow::~ConsoleWindow()
delete ui;
}
void ConsoleWindow::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
switch (reason)
{
case QSystemTrayIcon::Trigger:
{
toggleConsole();
}
default:
return;
}
}
void ConsoleWindow::writeColor(QString text, const char *color)
{
// append a paragraph
@ -76,6 +105,8 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
QScrollBar *bar = ui->text->verticalScrollBar();
int max_bar = bar->maximum();
int val_bar = bar->value();
if(isVisible())
{
if (m_scroll_active)
{
m_scroll_active = (max_bar - val_bar) <= 1;
@ -84,7 +115,7 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
{
m_scroll_active = val_bar == max_bar;
}
}
if (data.endsWith('\n'))
data = data.left(data.length() - 1);
QStringList paragraphs = data.split('\n');
@ -113,12 +144,15 @@ void ConsoleWindow::write(QString data, MessageLevel::Enum mode)
else
while (iter.hasNext())
writeColor(iter.next());
if(isVisible())
{
if (m_scroll_active)
{
bar->setValue(bar->maximum());
}
m_last_scroll_value = bar->value();
}
}
void ConsoleWindow::clear()
{
@ -133,22 +167,45 @@ void ConsoleWindow::on_closeButton_clicked()
void ConsoleWindow::setMayClose(bool mayclose)
{
m_mayclose = mayclose;
if (mayclose)
ui->closeButton->setEnabled(true);
}
void ConsoleWindow::toggleConsole()
{
QScrollBar *bar = ui->text->verticalScrollBar();
if (isVisible())
{
int max_bar = bar->maximum();
int val_bar = m_last_scroll_value = bar->value();
m_scroll_active = (max_bar - val_bar) <= 1;
hide();
}
else
ui->closeButton->setEnabled(false);
{
show();
if (m_scroll_active)
{
bar->setValue(bar->maximum());
}
else
{
bar->setValue(m_last_scroll_value);
}
}
}
void ConsoleWindow::closeEvent(QCloseEvent *event)
{
if (!m_mayclose)
event->ignore();
{
toggleConsole();
}
else
{
MMC->settings()->set("ConsoleWindowState", saveState().toBase64());
MMC->settings()->set("ConsoleWindowGeometry", saveGeometry().toBase64());
emit isClosing();
m_trayIcon->hide();
QMainWindow::closeEvent(event);
}
}
@ -169,18 +226,25 @@ void ConsoleWindow::on_btnKillMinecraft_clicked()
void ConsoleWindow::onEnded(BaseInstance *instance, int code, QProcess::ExitStatus status)
{
bool peacefulExit = code == 0 && status != QProcess::CrashExit;
ui->btnKillMinecraft->setEnabled(false);
setMayClose(true);
if (instance->settings().get("AutoCloseConsole").toBool())
{
if (code == 0 && status != QProcess::CrashExit)
if (peacefulExit)
{
this->close();
return;
}
}
/*
if(!peacefulExit)
{
m_trayIcon->showMessage(tr("Oh no!"), tr("Minecraft crashed!"), QSystemTrayIcon::Critical);
}
*/
if (!isVisible())
show();
}
@ -203,6 +267,7 @@ void ConsoleWindow::on_btnPaste_clicked()
dialog.exec(paste);
if (!paste->successful())
{
CustomMessageBox::selectable(this, "Upload failed", paste->failReason(), QMessageBox::Critical)->exec();
CustomMessageBox::selectable(this, "Upload failed", paste->failReason(),
QMessageBox::Critical)->exec();
}
}

View File

@ -16,6 +16,7 @@
#pragma once
#include <QMainWindow>
#include <QSystemTrayIcon>
#include "logic/MinecraftProcess.h"
namespace Ui
@ -77,7 +78,8 @@ slots:
// failures)
void on_btnPaste_clicked();
void iconActivated(QSystemTrayIcon::ActivationReason);
void toggleConsole();
protected:
void closeEvent(QCloseEvent *);
@ -87,4 +89,6 @@ private:
bool m_mayclose = true;
int m_last_scroll_value = 0;
bool m_scroll_active = true;
QSystemTrayIcon *m_trayIcon = nullptr;
int m_saved_offset = 0;
};