2013-11-02 01:20:02 +00:00
|
|
|
/*
|
2013-11-02 14:23:53 +00:00
|
|
|
* Copyright (c) 2013 Maximilian Güntner <maximilian.guentner@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of version 2 of
|
|
|
|
* the GNU General Public License. See the file gpl-2.0.txt in the main
|
|
|
|
* directory of this archive for more details.
|
|
|
|
*
|
|
|
|
* Original License:
|
|
|
|
*
|
2013-11-02 01:20:02 +00:00
|
|
|
* This file is part of the Nepomuk widgets collection
|
|
|
|
* Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License version 2.1 as published by the Free Software Foundation,
|
|
|
|
* or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public License
|
|
|
|
* along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "groupedlineedit.h"
|
|
|
|
|
2013-11-22 17:44:28 +00:00
|
|
|
#include <QScrollBar>
|
|
|
|
#include <QTextBlock>
|
|
|
|
#include <QPainter>
|
2013-11-02 01:20:02 +00:00
|
|
|
|
2013-11-02 14:23:53 +00:00
|
|
|
struct GroupedLineEdit::Private {
|
|
|
|
struct Block {
|
|
|
|
int start;
|
|
|
|
int end;
|
|
|
|
QString text;
|
|
|
|
};
|
|
|
|
QVector<Block> blocks;
|
|
|
|
QVector<QColor> colors;
|
2013-11-02 01:20:02 +00:00
|
|
|
};
|
|
|
|
|
2014-02-28 04:09:57 +00:00
|
|
|
GroupedLineEdit::GroupedLineEdit(QWidget *parent) : QPlainTextEdit(parent),
|
|
|
|
d(new Private)
|
2013-11-02 01:20:02 +00:00
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
setWordWrapMode(QTextOption::NoWrap);
|
|
|
|
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
|
2013-11-02 01:20:02 +00:00
|
|
|
|
2013-11-02 14:23:53 +00:00
|
|
|
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
|
|
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
2013-11-02 01:20:02 +00:00
|
|
|
|
2013-11-02 14:23:53 +00:00
|
|
|
document()->setMaximumBlockCount(1);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GroupedLineEdit::~GroupedLineEdit()
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
delete d;
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString GroupedLineEdit::text() const
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
// Remove the block crosses from the text
|
|
|
|
return toPlainText();
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GroupedLineEdit::cursorPosition() const
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
return textCursor().positionInBlock();
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::addBlock(int start, int end)
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
Private::Block block;
|
|
|
|
block.start = start;
|
|
|
|
block.end = end;
|
2014-07-10 16:24:57 +00:00
|
|
|
block.text = text().mid(start, end - start + 1).remove(',').trimmed();
|
2014-07-10 16:19:07 +00:00
|
|
|
if (block.text.isEmpty())
|
|
|
|
return;
|
2013-11-02 14:23:53 +00:00
|
|
|
d->blocks.append(block);
|
|
|
|
viewport()->update();
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::addColor(QColor color)
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
d->colors.append(color);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::removeAllColors()
|
|
|
|
{
|
|
|
|
d->colors.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList GroupedLineEdit::getBlockStringList()
|
|
|
|
{
|
|
|
|
QStringList retList;
|
2014-07-29 16:06:30 +00:00
|
|
|
foreach (const Private::Block &block, d->blocks)
|
2014-03-03 21:25:55 +00:00
|
|
|
retList.append(block.text);
|
2013-11-02 01:20:02 +00:00
|
|
|
return retList;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::setCursorPosition(int position)
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
QTextCursor c = textCursor();
|
|
|
|
c.setPosition(position, QTextCursor::MoveAnchor);
|
|
|
|
setTextCursor(c);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::setText(const QString &text)
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
setPlainText(text);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::clear()
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
QPlainTextEdit::clear();
|
|
|
|
removeAllBlocks();
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::selectAll()
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
QTextCursor c = textCursor();
|
|
|
|
c.select(QTextCursor::LineUnderCursor);
|
|
|
|
setTextCursor(c);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::removeAllBlocks()
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
d->blocks.clear();
|
|
|
|
viewport()->update();
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize GroupedLineEdit::sizeHint() const
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
QSize rs(
|
2014-05-22 18:40:22 +00:00
|
|
|
40,
|
|
|
|
document()->findBlock(0).layout()->lineAt(0).height() +
|
|
|
|
document()->documentMargin() * 2 +
|
|
|
|
frameWidth() * 2);
|
2013-11-02 14:23:53 +00:00
|
|
|
return rs;
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QSize GroupedLineEdit::minimumSizeHint() const
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
return sizeHint();
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::keyPressEvent(QKeyEvent *e)
|
|
|
|
{
|
2013-11-02 14:23:53 +00:00
|
|
|
switch (e->key()) {
|
|
|
|
case Qt::Key_Return:
|
|
|
|
case Qt::Key_Enter:
|
|
|
|
emit editingFinished();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QPlainTextEdit::keyPressEvent(e);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupedLineEdit::paintEvent(QPaintEvent *e)
|
|
|
|
{
|
2014-12-23 17:36:45 +00:00
|
|
|
// for reasons we don't understand, touching the painter
|
2013-11-22 17:44:28 +00:00
|
|
|
// here (even drawing the fill rect) causes the QPlainTextEdit
|
2014-12-23 17:36:45 +00:00
|
|
|
// paintEvent to not draw the text on Qt4 & MacOS.
|
2013-11-02 14:23:53 +00:00
|
|
|
QTextLine line = document()->findBlock(0).layout()->lineAt(0);
|
|
|
|
QPainter painter(viewport());
|
|
|
|
|
|
|
|
painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
|
painter.fillRect(0, 0, viewport()->width(), viewport()->height(), palette().base());
|
|
|
|
|
|
|
|
QVectorIterator<QColor> i(d->colors);
|
|
|
|
i.toFront();
|
2014-05-22 18:40:22 +00:00
|
|
|
foreach (const Private::Block &block, d->blocks) {
|
2014-05-27 23:06:24 +00:00
|
|
|
qreal start_x = line.cursorToX(block.start, QTextLine::Leading);
|
|
|
|
qreal end_x = line.cursorToX(block.end-1, QTextLine::Trailing);
|
2015-05-24 16:17:48 +00:00
|
|
|
|
2013-11-02 14:23:53 +00:00
|
|
|
QPainterPath path;
|
|
|
|
QRectF rectangle(
|
2014-05-22 18:40:22 +00:00
|
|
|
start_x - 1.0 - double(horizontalScrollBar()->value()),
|
|
|
|
1.0,
|
|
|
|
end_x - start_x + 2.0,
|
|
|
|
double(viewport()->height() - 2));
|
2014-02-28 04:09:57 +00:00
|
|
|
if (!i.hasNext())
|
2013-11-02 14:23:53 +00:00
|
|
|
i.toFront();
|
|
|
|
path.addRoundedRect(rectangle, 5.0, 5.0);
|
|
|
|
painter.setPen(i.peekNext());
|
2014-02-28 04:09:57 +00:00
|
|
|
if (palette().color(QPalette::Text).lightnessF() <= 0.3)
|
2013-12-19 19:16:57 +00:00
|
|
|
painter.setBrush(i.next().lighter());
|
2014-02-28 04:09:57 +00:00
|
|
|
else if (palette().color(QPalette::Text).lightnessF() <= 0.6)
|
2013-12-19 19:16:57 +00:00
|
|
|
painter.setBrush(i.next());
|
|
|
|
else
|
|
|
|
painter.setBrush(i.next().darker());
|
2013-11-02 14:23:53 +00:00
|
|
|
painter.drawPath(path);
|
|
|
|
}
|
|
|
|
QPlainTextEdit::paintEvent(e);
|
2013-11-02 01:20:02 +00:00
|
|
|
}
|