Implement ConsoleWindow
This commit is contained in:
73
gui/consolewindow.cpp
Normal file
73
gui/consolewindow.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
#include "consolewindow.h"
|
||||
#include "ui_consolewindow.h"
|
||||
|
||||
#include <QScrollBar>
|
||||
|
||||
ConsoleWindow::ConsoleWindow(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::ConsoleWindow),
|
||||
m_mayclose(true)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
ConsoleWindow::~ConsoleWindow()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void ConsoleWindow::writeColor(QString text, const char *color)
|
||||
{
|
||||
// append a paragraph
|
||||
if (color != nullptr)
|
||||
ui->text->appendHtml(QString("<font color=%1>%2</font>").arg(color).arg(text));
|
||||
else
|
||||
ui->text->appendPlainText(text);
|
||||
// scroll down
|
||||
QScrollBar *bar = ui->text->verticalScrollBar();
|
||||
bar->setValue(bar->maximum());
|
||||
}
|
||||
|
||||
void ConsoleWindow::write(QString data, WriteMode mode)
|
||||
{
|
||||
if (data.endsWith('\n'))
|
||||
data = data.left(data.length()-1);
|
||||
QStringList paragraphs = data.split('\n');
|
||||
QListIterator<QString> iter(paragraphs);
|
||||
if (mode == MULTIMC)
|
||||
while(iter.hasNext())
|
||||
writeColor(iter.next(), "blue");
|
||||
else if (mode == ERROR)
|
||||
while(iter.hasNext())
|
||||
writeColor(iter.next(), "red");
|
||||
else
|
||||
while(iter.hasNext())
|
||||
writeColor(iter.next());
|
||||
}
|
||||
|
||||
void ConsoleWindow::clear()
|
||||
{
|
||||
ui->text->clear();
|
||||
}
|
||||
|
||||
void ConsoleWindow::on_closeButton_clicked()
|
||||
{
|
||||
close();
|
||||
}
|
||||
|
||||
void ConsoleWindow::setMayClose(bool mayclose)
|
||||
{
|
||||
m_mayclose = mayclose;
|
||||
if (mayclose)
|
||||
ui->closeButton->setEnabled(true);
|
||||
else
|
||||
ui->closeButton->setEnabled(false);
|
||||
}
|
||||
|
||||
void ConsoleWindow::closeEvent(QCloseEvent * event)
|
||||
{
|
||||
if(!m_mayclose)
|
||||
event->ignore();
|
||||
else
|
||||
QDialog::closeEvent(event);
|
||||
}
|
69
gui/consolewindow.h
Normal file
69
gui/consolewindow.h
Normal file
@ -0,0 +1,69 @@
|
||||
#ifndef CONSOLEWINDOW_H
|
||||
#define CONSOLEWINDOW_H
|
||||
|
||||
#include <QDialog>
|
||||
|
||||
namespace Ui {
|
||||
class ConsoleWindow;
|
||||
}
|
||||
|
||||
class ConsoleWindow : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief The WriteMode enum
|
||||
* defines how stuff is displayed
|
||||
*/
|
||||
enum WriteMode {
|
||||
DEFAULT,
|
||||
ERROR,
|
||||
MULTIMC
|
||||
};
|
||||
|
||||
explicit ConsoleWindow(QWidget *parent = 0);
|
||||
~ConsoleWindow();
|
||||
|
||||
/**
|
||||
* @brief specify if the window is allowed to close
|
||||
* @param mayclose
|
||||
* used to keep it alive while MC runs
|
||||
*/
|
||||
void setMayClose(bool mayclose);
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* @brief write a string
|
||||
* @param data the string
|
||||
* @param mode the WriteMode
|
||||
* lines have to be put through this as a whole!
|
||||
*/
|
||||
void write(QString data, WriteMode mode=MULTIMC);
|
||||
|
||||
/**
|
||||
* @brief write a colored paragraph
|
||||
* @param data the string
|
||||
* @param color the css color name
|
||||
* this will only insert a single paragraph.
|
||||
* \n are ignored. a real \n is always appended.
|
||||
*/
|
||||
void writeColor(QString data, const char *color=nullptr);
|
||||
|
||||
/**
|
||||
* @brief clear the text widget
|
||||
*/
|
||||
void clear();
|
||||
|
||||
private slots:
|
||||
void on_closeButton_clicked();
|
||||
|
||||
protected:
|
||||
void closeEvent(QCloseEvent *);
|
||||
|
||||
private:
|
||||
Ui::ConsoleWindow *ui;
|
||||
bool m_mayclose;
|
||||
};
|
||||
|
||||
#endif // CONSOLEWINDOW_H
|
66
gui/consolewindow.ui
Normal file
66
gui/consolewindow.ui
Normal file
@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>ConsoleWindow</class>
|
||||
<widget class="QDialog" name="ConsoleWindow">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>600</width>
|
||||
<height>400</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>MultiMC Console</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QPlainTextEdit" name="text">
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="undoRedoEnabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
<property name="plainText">
|
||||
<string notr="true"/>
|
||||
</property>
|
||||
<property name="centerOnScroll">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<spacer name="horizontalSpacer">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Horizontal</enum>
|
||||
</property>
|
||||
<property name="sizeHint" stdset="0">
|
||||
<size>
|
||||
<width>40</width>
|
||||
<height>20</height>
|
||||
</size>
|
||||
</property>
|
||||
</spacer>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="closeButton">
|
||||
<property name="text">
|
||||
<string>Close</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
Reference in New Issue
Block a user