Commit 03aa59bf authored by Nikita Sirgienko's avatar Nikita Sirgienko
Browse files

[Feature] Add collapsing of text results with a lot of visible lines (limit of...

[Feature] Add collapsing of text results with a lot of visible lines (limit of collapsing set in Settings). Double click can collapse/uncollapse collapsed text result.
parent e72c6da1
......@@ -50,6 +50,10 @@
<label>Show embedded math render error</label>
<default>true</default>
</entry>
<entry name="VisibleLinesLimit" type="Int">
<label>Limit of visible lines for text result</label>
<default>0</default>
</entry>
</group>
</kcfg>
<?xml version="1.0" encoding="UTF-8"?>
<ui version="6.0">
<ui version="4.0">
<class>SettingsBase</class>
<widget class="QWidget" name="SettingsBase">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>398</width>
<height>400</height>
<width>845</width>
<height>631</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
......@@ -103,6 +103,32 @@
</property>
</widget>
</item>
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<widget class="QLabel" name="VisibleLinesLimit_label">
<property name="toolTip">
<string>Maximal number of visible rows in the result of a command entry. Use 0 to work without limitations.</string>
</property>
<property name="text">
<string>Maximal number of result visible rows (0 for unlimited):</string>
</property>
</widget>
<widget class="QSpinBox" name="kcfg_VisibleLinesLimit">
<property name="toolTip">
<string/>
</property>
<property name="maximum">
<number>10000</number>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
</item>
......
......@@ -28,6 +28,7 @@
#include "lib/htmlresult.h"
#include "mathrendertask.h"
#include "config-cantor.h"
#include "settings.h"
#include <QDebug>
#include <QFileDialog>
......@@ -39,6 +40,7 @@
TextResultItem::TextResultItem(QGraphicsObject* parent, Cantor::Result* result)
: WorksheetTextItem(parent), ResultItem(result)
{
connect(this, SIGNAL(collapseActionSizeChanged()), parent, SLOT(recalculateSize()));
setTextInteractionFlags(Qt::TextSelectableByMouse);
update();
......@@ -56,7 +58,9 @@ TextResultItem::TextResultItem(QGraphicsObject* parent, Cantor::Result* result)
double TextResultItem::setGeometry(double x, double y, double w)
{
return WorksheetTextItem::setGeometry(x, y, w);
WorksheetTextItem::setGeometry(x, y, w);
collapseExtraLines();
return height();
}
void TextResultItem::populateMenu(QMenu* menu, QPointF pos)
......@@ -246,3 +250,94 @@ QTextImageFormat TextResultItem::toFormat(const QImage& image, const QString& la
return format;
}
void TextResultItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event)
{
if (!m_isCollapsed)
return;
m_userCollapseOverride = !m_userCollapseOverride;
if (m_isCollapsed)
{
if (m_userCollapseOverride)
{
update();
}
else
{
m_isCollapsed = false;
collapseExtraLines();
}
emit collapseActionSizeChanged();
}
QGraphicsTextItem::mouseDoubleClickEvent(event);
}
int TextResultItem::visibleLineCount()
{
int lineCounter = 0;
QTextCursor cursor(document());
if(!cursor.isNull())
{
cursor.movePosition(QTextCursor::Start);
bool isNotDone = true;
while( isNotDone )
{
isNotDone = cursor.movePosition( QTextCursor::Down );
lineCounter++;
}
}
return lineCounter;
}
void TextResultItem::collapseExtraLines()
{
if (m_userCollapseOverride)
return;
int limit = Settings::visibleLinesLimit();
// If limit disable (0 is for unlimited mode), then exit
if (limit == 0)
return;
// for situation, when we have collapsed text result and resized Cantor window
if (m_isCollapsed && (int)width() != m_widthWhenCollapsed)
{
update();
m_isCollapsed = false;
}
if (visibleLineCount() > limit)
{
QTextCursor cursor(document());
cursor.movePosition(QTextCursor::Start);
if (limit > 4)
{
for (int i = 0; i < limit-4; i++)
cursor.movePosition(QTextCursor::Down);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
cursor.movePosition(QTextCursor::Up, QTextCursor::KeepAnchor);
cursor.insertText(QLatin1String("\n\n...\n\n"));
}
else
{
for (int i = 0; i < limit-1; i++)
cursor.movePosition(QTextCursor::Down);
cursor.movePosition(QTextCursor::EndOfLine);
QString replacer = QLatin1String("...");
for (int i = 0; i < replacer.length(); i++)
cursor.movePosition(QTextCursor::Left);
cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
cursor.insertText(replacer);
}
m_isCollapsed = true;
m_widthWhenCollapsed = (int)width();
}
}
......@@ -48,12 +48,25 @@ class TextResultItem : public WorksheetTextItem, public ResultItem
void deleteLater() override;
Q_SIGNALS:
void collapseActionSizeChanged();
protected Q_SLOTS:
void toggleLatexCode();
void showHtml();
void showHtmlSource();
void showPlain();
void saveResult();
protected:
void mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event) override;
int visibleLineCount();
void collapseExtraLines();
protected:
bool m_isCollapsed{false};
bool m_userCollapseOverride{false};
int m_widthWhenCollapsed{0};
};
#endif //TEXTRESULTITEM_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment