Implement search and logging pause in minecraft log

Fixes #47
This commit is contained in:
Petr Mrázek 2014-11-09 00:59:22 +01:00
parent fa42a27525
commit 587fedce84
3 changed files with 92 additions and 27 deletions

View File

@ -3,6 +3,7 @@
#include <QIcon> #include <QIcon>
#include <QScrollBar> #include <QScrollBar>
#include <QShortcut>
#include "logic/MinecraftProcess.h" #include "logic/MinecraftProcess.h"
#include "gui/GuiUtil.h" #include "gui/GuiUtil.h"
@ -13,7 +14,15 @@ LogPage::LogPage(MinecraftProcess *proc, QWidget *parent)
ui->setupUi(this); ui->setupUi(this);
ui->tabWidget->tabBar()->hide(); ui->tabWidget->tabBar()->hide();
connect(m_process, SIGNAL(log(QString, MessageLevel::Enum)), this, connect(m_process, SIGNAL(log(QString, MessageLevel::Enum)), this,
SLOT(write(QString, MessageLevel::Enum))); SLOT(write(QString, MessageLevel::Enum)));
auto findShortcut = new QShortcut(QKeySequence(QKeySequence::Find), this);
connect(findShortcut, SIGNAL(activated()), SLOT(findActivated()));
auto findNextShortcut = new QShortcut(QKeySequence(QKeySequence::FindNext), this);
connect(findNextShortcut, SIGNAL(activated()), SLOT(findNextActivated()));
connect(ui->searchBar, SIGNAL(returnPressed()), SLOT(on_findButton_clicked()));
auto findPreviousShortcut = new QShortcut(QKeySequence(QKeySequence::FindPrevious), this);
connect(findPreviousShortcut, SIGNAL(activated()), SLOT(findPreviousActivated()));
} }
LogPage::~LogPage() LogPage::~LogPage()
@ -46,7 +55,59 @@ void LogPage::on_btnClear_clicked()
ui->text->clear(); ui->text->clear();
} }
void LogPage::writeColor(QString text, const char *color, const char * background) void LogPage::on_trackLogCheckbox_clicked(bool checked)
{
m_write_active = checked;
}
void LogPage::on_findButton_clicked()
{
auto modifiers = QApplication::keyboardModifiers();
if (modifiers & Qt::ShiftModifier)
{
findPreviousActivated();
}
else
{
findNextActivated();
}
}
void LogPage::findActivated()
{
// focus the search bar if it doesn't have focus
if (!ui->searchBar->hasFocus())
{
auto searchForCursor = ui->text->textCursor();
auto searchForString = searchForCursor.selectedText();
if (searchForString.size())
{
ui->searchBar->setText(searchForString);
}
ui->searchBar->setFocus();
ui->searchBar->selectAll();
}
}
void LogPage::findNextActivated()
{
auto toSearch = ui->searchBar->text();
if (toSearch.size())
{
ui->text->find(toSearch);
}
}
void LogPage::findPreviousActivated()
{
auto toSearch = ui->searchBar->text();
if (toSearch.size())
{
ui->text->find(toSearch, QTextDocument::FindBackward);
}
}
void LogPage::writeColor(QString text, const char *color, const char *background)
{ {
// append a paragraph // append a paragraph
QString newtext; QString newtext;
@ -66,10 +127,21 @@ void LogPage::writeColor(QString text, const char *color, const char * backgroun
void LogPage::write(QString data, MessageLevel::Enum mode) void LogPage::write(QString data, MessageLevel::Enum mode)
{ {
if (!m_write_active)
{
if (mode != MessageLevel::PrePost && mode != MessageLevel::MultiMC)
{
return;
}
}
// save the cursor so it can be restored.
auto savedCursor = ui->text->cursor();
QScrollBar *bar = ui->text->verticalScrollBar(); QScrollBar *bar = ui->text->verticalScrollBar();
int max_bar = bar->maximum(); int max_bar = bar->maximum();
int val_bar = bar->value(); int val_bar = bar->value();
if(isVisible()) if (isVisible())
{ {
if (m_scroll_active) if (m_scroll_active)
{ {
@ -86,9 +158,7 @@ void LogPage::write(QString data, MessageLevel::Enum mode)
QStringList filtered; QStringList filtered;
for (QString &paragraph : paragraphs) for (QString &paragraph : paragraphs)
{ {
// Quick hack for //TODO: implement filtering here.
if(paragraph.contains("Detected an attempt by a mod null to perform game activity during mod construction"))
continue;
filtered.append(paragraph.trimmed()); filtered.append(paragraph.trimmed());
} }
QListIterator<QString> iter(filtered); QListIterator<QString> iter(filtered);
@ -114,7 +184,7 @@ void LogPage::write(QString data, MessageLevel::Enum mode)
else else
while (iter.hasNext()) while (iter.hasNext())
writeColor(iter.next(), 0, 0); writeColor(iter.next(), 0, 0);
if(isVisible()) if (isVisible())
{ {
if (m_scroll_active) if (m_scroll_active)
{ {
@ -122,4 +192,5 @@ void LogPage::write(QString data, MessageLevel::Enum mode)
} }
m_last_scroll_value = bar->value(); m_last_scroll_value = bar->value();
} }
ui->text->setCursor(savedCursor);
} }

View File

@ -64,7 +64,6 @@ private:
* \n are ignored. a real \n is always appended. * \n are ignored. a real \n is always appended.
*/ */
void writeColor(QString text, const char *color, const char *background); void writeColor(QString text, const char *color, const char *background);
private slots: private slots:
/** /**
* @brief write a string * @brief write a string
@ -77,10 +76,18 @@ private slots:
void on_btnCopy_clicked(); void on_btnCopy_clicked();
void on_btnClear_clicked(); void on_btnClear_clicked();
void on_trackLogCheckbox_clicked(bool checked);
void on_findButton_clicked();
void findActivated();
void findNextActivated();
void findPreviousActivated();
private: private:
Ui::LogPage *ui; Ui::LogPage *ui;
MinecraftProcess *m_process; MinecraftProcess *m_process;
int m_last_scroll_value = 0; int m_last_scroll_value = 0;
bool m_scroll_active = true; bool m_scroll_active = true;
int m_saved_offset = 0; int m_saved_offset = 0;
bool m_write_active = true;
}; };

View File

@ -38,28 +38,15 @@
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="2" column="0"> <item row="2" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>Search:</string> <string>Search:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QLineEdit" name="lineEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="2"> <item row="2" column="2">
<widget class="QPushButton" name="pushButton"> <widget class="QPushButton" name="findButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>Find next</string> <string>Find</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -85,10 +72,7 @@
<item row="0" column="0" colspan="3"> <item row="0" column="0" colspan="3">
<layout class="QHBoxLayout" name="horizontalLayout"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
<widget class="QCheckBox" name="checkBox"> <widget class="QCheckBox" name="trackLogCheckbox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>Keep updating</string> <string>Keep updating</string>
</property> </property>
@ -142,6 +126,9 @@
</item> </item>
</layout> </layout>
</item> </item>
<item row="2" column="1">
<widget class="QLineEdit" name="searchBar"/>
</item>
</layout> </layout>
</widget> </widget>
</widget> </widget>