2017-10-01 19:09:42 +00:00
|
|
|
#include "utils/Helpers.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2018-02-04 17:27:48 +00:00
|
|
|
#include <cmath>
|
2017-04-05 08:56:59 +00:00
|
|
|
#include <QPlainTextEdit>
|
|
|
|
#include <QTextEdit>
|
2017-04-18 10:03:47 +00:00
|
|
|
#include <QFileInfo>
|
2018-02-04 17:27:48 +00:00
|
|
|
#include <QtCore>
|
2017-04-18 10:03:47 +00:00
|
|
|
#include <QCryptographicHash>
|
2017-04-13 15:04:30 +00:00
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QString>
|
|
|
|
#include <QAbstractItemView>
|
2017-10-01 14:36:40 +00:00
|
|
|
#include <QAbstractButton>
|
2017-11-03 17:22:54 +00:00
|
|
|
#include <QDockWidget>
|
2017-04-05 08:56:59 +00:00
|
|
|
|
2017-04-13 15:04:30 +00:00
|
|
|
|
2018-02-04 17:27:48 +00:00
|
|
|
|
2017-04-13 15:04:30 +00:00
|
|
|
static QAbstractItemView::ScrollMode scrollMode()
|
2017-03-29 10:18:37 +00:00
|
|
|
{
|
2017-04-13 15:04:30 +00:00
|
|
|
const bool use_scrollperpixel = true;
|
|
|
|
return use_scrollperpixel ? QAbstractItemView::ScrollPerPixel : QAbstractItemView::ScrollPerItem;
|
|
|
|
}
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-04-13 15:04:30 +00:00
|
|
|
|
|
|
|
namespace qhelpers
|
|
|
|
{
|
2018-02-04 17:27:48 +00:00
|
|
|
|
|
|
|
QString formatBytecount(const long bytecount)
|
|
|
|
{
|
2018-02-10 22:31:15 +00:00
|
|
|
if (bytecount == 0)
|
|
|
|
return "0";
|
2018-02-04 17:27:48 +00:00
|
|
|
const int exp = log(bytecount) / log(1000);
|
|
|
|
constexpr char suffixes[] = {' ', 'k', 'M', 'G', 'T', 'P', 'E'};
|
|
|
|
|
|
|
|
QString str;
|
|
|
|
QTextStream stream(&str);
|
|
|
|
stream << qSetRealNumberPrecision(3) << bytecount / pow(1000, exp)
|
|
|
|
<< ' ' << suffixes[exp] << 'B';
|
|
|
|
return stream.readAll();
|
|
|
|
}
|
2017-12-23 16:42:42 +00:00
|
|
|
void adjustColumns(QTreeView *tv, int columnCount, int padding)
|
2017-04-13 15:04:30 +00:00
|
|
|
{
|
2017-12-23 16:42:42 +00:00
|
|
|
for (int i = 0; i != columnCount; ++i)
|
2017-04-13 15:04:30 +00:00
|
|
|
{
|
2017-12-23 16:42:42 +00:00
|
|
|
tv->resizeColumnToContents(i);
|
2017-04-13 16:18:56 +00:00
|
|
|
if (padding > 0)
|
|
|
|
{
|
2017-12-23 16:42:42 +00:00
|
|
|
int width = tv->columnWidth(i);
|
|
|
|
tv->setColumnWidth(i, width + padding);
|
2017-04-13 16:18:56 +00:00
|
|
|
}
|
2017-04-13 15:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 16:42:42 +00:00
|
|
|
void adjustColumns(QTreeWidget *tw, int padding)
|
|
|
|
{
|
|
|
|
adjustColumns(tw, tw->columnCount(), padding);
|
|
|
|
}
|
|
|
|
|
2017-04-28 13:09:40 +00:00
|
|
|
QTreeWidgetItem *appendRow(QTreeWidget *tw, const QString &str, const QString &str2,
|
2017-04-28 13:38:01 +00:00
|
|
|
const QString &str3, const QString &str4, const QString &str5)
|
2017-04-13 15:04:30 +00:00
|
|
|
{
|
|
|
|
QTreeWidgetItem *tempItem = new QTreeWidgetItem();
|
|
|
|
// Fill dummy hidden column
|
|
|
|
tempItem->setText(0, "0");
|
|
|
|
tempItem->setText(1, str);
|
|
|
|
if (!str2.isNull())
|
|
|
|
tempItem->setText(2, str2);
|
|
|
|
if (!str3.isNull())
|
|
|
|
tempItem->setText(3, str3);
|
|
|
|
if (!str4.isNull())
|
|
|
|
tempItem->setText(4, str4);
|
|
|
|
if (!str5.isNull())
|
|
|
|
tempItem->setText(5, str5);
|
|
|
|
|
|
|
|
tw->insertTopLevelItem(0, tempItem);
|
2017-04-28 13:09:40 +00:00
|
|
|
|
|
|
|
return tempItem;
|
2017-04-13 15:04:30 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:09:40 +00:00
|
|
|
void setVerticalScrollMode(QAbstractItemView *tw)
|
2017-04-13 15:04:30 +00:00
|
|
|
{
|
|
|
|
tw->setVerticalScrollMode(scrollMode());
|
|
|
|
}
|
|
|
|
|
2017-10-01 14:36:40 +00:00
|
|
|
void setCheckedWithoutSignals(QAbstractButton *button, bool checked)
|
|
|
|
{
|
|
|
|
bool blocked = button->signalsBlocked();
|
|
|
|
button->blockSignals(true);
|
|
|
|
button->setChecked(checked);
|
|
|
|
button->blockSignals(blocked);
|
|
|
|
}
|
|
|
|
|
2017-11-03 17:22:54 +00:00
|
|
|
|
|
|
|
SizePolicyMinMax forceWidth(QWidget *widget, int width)
|
|
|
|
{
|
|
|
|
SizePolicyMinMax r;
|
|
|
|
r.sizePolicy = widget->sizePolicy();
|
|
|
|
r.min = widget->minimumWidth();
|
|
|
|
r.max = widget->maximumWidth();
|
|
|
|
|
|
|
|
QSizePolicy sizePolicy = r.sizePolicy;
|
|
|
|
sizePolicy.setHorizontalPolicy(QSizePolicy::Fixed);
|
|
|
|
widget->setSizePolicy(sizePolicy);
|
|
|
|
widget->setMinimumWidth(width);
|
|
|
|
widget->setMaximumWidth(width);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SizePolicyMinMax forceHeight(QWidget *widget, int height)
|
|
|
|
{
|
|
|
|
SizePolicyMinMax r;
|
|
|
|
r.sizePolicy = widget->sizePolicy();
|
|
|
|
r.min = widget->minimumHeight();
|
|
|
|
r.max = widget->maximumHeight();
|
|
|
|
|
|
|
|
QSizePolicy sizePolicy = r.sizePolicy;
|
|
|
|
sizePolicy.setVerticalPolicy(QSizePolicy::Fixed);
|
|
|
|
widget->setSizePolicy(sizePolicy);
|
|
|
|
widget->setMinimumHeight(height);
|
|
|
|
widget->setMaximumHeight(height);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SizePolicyMinMax::restoreWidth(QWidget *widget)
|
|
|
|
{
|
|
|
|
widget->setSizePolicy(sizePolicy);
|
|
|
|
widget->setMinimumWidth(min);
|
|
|
|
widget->setMaximumWidth(max);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SizePolicyMinMax::restoreHeight(QWidget *widget)
|
|
|
|
{
|
|
|
|
widget->setSizePolicy(sizePolicy);
|
|
|
|
widget->setMinimumHeight(min);
|
|
|
|
widget->setMaximumHeight(max);
|
|
|
|
}
|
|
|
|
|
2018-01-27 10:08:05 +00:00
|
|
|
int getMaxFullyDisplayedLines(QTextEdit *textEdit)
|
|
|
|
{
|
|
|
|
QFontMetrics fontMetrics(textEdit->document()->defaultFont());
|
|
|
|
return (textEdit->height()
|
|
|
|
- (textEdit->contentsMargins().top()
|
|
|
|
+ textEdit->contentsMargins().bottom()
|
|
|
|
+ (int)(textEdit->document()->documentMargin() * 2)))
|
|
|
|
/ fontMetrics.lineSpacing();
|
|
|
|
}
|
2017-11-15 21:56:10 +00:00
|
|
|
|
|
|
|
int getMaxFullyDisplayedLines(QPlainTextEdit *plainTextEdit)
|
|
|
|
{
|
|
|
|
QFontMetrics fontMetrics(plainTextEdit->document()->defaultFont());
|
|
|
|
return (plainTextEdit->height()
|
|
|
|
- (plainTextEdit->contentsMargins().top()
|
|
|
|
+ plainTextEdit->contentsMargins().bottom()
|
|
|
|
+ (int)(plainTextEdit->document()->documentMargin() * 2)))
|
|
|
|
/ fontMetrics.lineSpacing();
|
|
|
|
}
|
|
|
|
|
2017-12-03 16:26:01 +00:00
|
|
|
QByteArray applyColorToSvg(const QByteArray &data, QColor color)
|
2017-12-03 14:46:22 +00:00
|
|
|
{
|
2017-12-03 16:26:01 +00:00
|
|
|
static const QRegularExpression styleRegExp("(?:style=\".*fill:(.*?);.*?\")|(?:fill=\"(.*?)\")");
|
2017-12-03 14:46:22 +00:00
|
|
|
|
|
|
|
QString replaceStr = QString("#%1").arg(color.rgb() & 0xffffff, 6, 16, QLatin1Char('0'));
|
|
|
|
int replaceStrLen = replaceStr.length();
|
|
|
|
|
2017-12-03 16:26:01 +00:00
|
|
|
QString xml = QString::fromUtf8(data);
|
2017-12-03 14:46:22 +00:00
|
|
|
|
|
|
|
int offset = 0;
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
QRegularExpressionMatch match = styleRegExp.match(xml, offset);
|
|
|
|
if (!match.hasMatch())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
int captureIndex = match.captured(1).isNull() ? 2 : 1;
|
|
|
|
xml.replace(match.capturedStart(captureIndex), match.capturedLength(captureIndex), replaceStr);
|
|
|
|
offset = match.capturedStart(captureIndex) + replaceStrLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
return xml.toUtf8();
|
|
|
|
}
|
|
|
|
|
2017-12-03 16:26:01 +00:00
|
|
|
QByteArray applyColorToSvg(const QString &filename, QColor color)
|
|
|
|
{
|
|
|
|
QFile file(filename);
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
return applyColorToSvg(file.readAll(), color);
|
|
|
|
}
|
|
|
|
|
2017-04-05 08:56:59 +00:00
|
|
|
} // end namespace
|