cutter/src/utils/Helpers.cpp

141 lines
3.8 KiB
C++
Raw Normal View History

2017-10-01 19:09:42 +00:00
#include "utils/Helpers.h"
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QFileInfo>
#include <QCryptographicHash>
2017-04-13 15:04:30 +00:00
#include <QTreeWidget>
#include <QString>
#include <QAbstractItemView>
#include <QAbstractButton>
#include <QDockWidget>
#include <QtGui/QtGui>
2017-04-13 15:04:30 +00:00
static QAbstractItemView::ScrollMode scrollMode()
{
2017-04-13 15:04:30 +00:00
const bool use_scrollperpixel = true;
return use_scrollperpixel ? QAbstractItemView::ScrollPerPixel : QAbstractItemView::ScrollPerItem;
}
2017-04-13 15:04:30 +00:00
namespace qhelpers
{
2017-04-09 19:55:06 +00:00
// TODO: wouldn't it be enough to setFont on the QWidget?
2017-04-09 19:55:06 +00:00
void normalizeFont(QPlainTextEdit *edit)
{
#ifdef Q_OS_LINUX
QFont anonFont("Inconsolata", 12);
QTextDocument *out_doc = edit->document();
out_doc->setDefaultFont(anonFont);
2017-04-09 19:55:06 +00:00
#endif
}
2017-04-09 19:55:06 +00:00
void normalizeEditFont(QTextEdit *edit)
{
#ifdef Q_OS_LINUX
QFont anonFont("Inconsolata", 12);
QTextDocument *out_doc = edit->document();
out_doc->setDefaultFont(anonFont);
2017-04-09 19:55:06 +00:00
#endif
}
void adjustColumns(QTreeWidget *tw, int columnCount, int padding)
2017-04-13 15:04:30 +00:00
{
const int count = columnCount == 0 ? tw->columnCount() : columnCount;
2017-04-13 15:04:30 +00:00
for (int i = 0; i != count; ++i)
{
tw->resizeColumnToContents(i);
if (padding > 0)
{
int width = tw->columnWidth(i);
tw->setColumnWidth(i, width + padding);
}
2017-04-13 15:04:30 +00:00
}
}
QTreeWidgetItem *appendRow(QTreeWidget *tw, const QString &str, const QString &str2,
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);
return tempItem;
2017-04-13 15:04:30 +00:00
}
void setVerticalScrollMode(QAbstractItemView *tw)
2017-04-13 15:04:30 +00:00
{
tw->setVerticalScrollMode(scrollMode());
}
void setCheckedWithoutSignals(QAbstractButton *button, bool checked)
{
bool blocked = button->signalsBlocked();
button->blockSignals(true);
button->setChecked(checked);
button->blockSignals(blocked);
}
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);
}
} // end namespace