Merge branch 'tagwidget-pr' of github.com:mguentner/subsurface

This commit is contained in:
Dirk Hohndel 2013-11-02 12:56:18 -07:00
commit 7966f72fb7
5 changed files with 146 additions and 83 deletions

View file

@ -1,4 +1,12 @@
/* /*
* 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:
*
* This file is part of the Nepomuk widgets collection * This file is part of the Nepomuk widgets collection
* Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr> * Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr>
* *
@ -34,61 +42,60 @@
#include <QtGui/QColor> #include <QtGui/QColor>
#include <QtGui/QPalette> #include <QtGui/QPalette>
struct GroupedLineEdit::Private struct GroupedLineEdit::Private {
{ struct Block {
struct Block { int start;
int start; int end;
int end; QString text;
QString text; };
}; QVector<Block> blocks;
QVector<Block> blocks; QVector<QColor> colors;
QVector<QColor> colors;
}; };
GroupedLineEdit::GroupedLineEdit(QWidget* parent) GroupedLineEdit::GroupedLineEdit(QWidget* parent)
: QPlainTextEdit(parent), : QPlainTextEdit(parent),
d(new Private) d(new Private)
{ {
setWordWrapMode(QTextOption::NoWrap); setWordWrapMode(QTextOption::NoWrap);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed); setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
document()->setMaximumBlockCount(1); document()->setMaximumBlockCount(1);
} }
GroupedLineEdit::~GroupedLineEdit() GroupedLineEdit::~GroupedLineEdit()
{ {
delete d; delete d;
} }
QString GroupedLineEdit::text() const QString GroupedLineEdit::text() const
{ {
// Remove the block crosses from the text // Remove the block crosses from the text
return toPlainText(); return toPlainText();
} }
int GroupedLineEdit::cursorPosition() const int GroupedLineEdit::cursorPosition() const
{ {
return textCursor().positionInBlock(); return textCursor().positionInBlock();
} }
void GroupedLineEdit::addBlock(int start, int end) void GroupedLineEdit::addBlock(int start, int end)
{ {
Private::Block block; Private::Block block;
block.start = start; block.start = start;
block.end = end; block.end = end;
block.text = text().mid(start, end-start+1).trimmed(); block.text = text().mid(start, end-start+1).trimmed();
d->blocks.append(block); d->blocks.append(block);
viewport()->update(); viewport()->update();
} }
void GroupedLineEdit::addColor(QColor color) void GroupedLineEdit::addColor(QColor color)
{ {
d->colors.append(color); d->colors.append(color);
} }
void GroupedLineEdit::removeAllColors() void GroupedLineEdit::removeAllColors()
@ -107,98 +114,95 @@ QStringList GroupedLineEdit::getBlockStringList()
void GroupedLineEdit::setCursorPosition(int position) void GroupedLineEdit::setCursorPosition(int position)
{ {
QTextCursor c = textCursor(); QTextCursor c = textCursor();
c.setPosition(position, QTextCursor::MoveAnchor);
c.setPosition(position, QTextCursor::MoveAnchor); setTextCursor(c);
setTextCursor(c);
} }
void GroupedLineEdit::setText(const QString &text) void GroupedLineEdit::setText(const QString &text)
{ {
setPlainText(text); setPlainText(text);
} }
void GroupedLineEdit::clear() void GroupedLineEdit::clear()
{ {
QPlainTextEdit::clear(); QPlainTextEdit::clear();
removeAllBlocks(); removeAllBlocks();
} }
void GroupedLineEdit::selectAll() void GroupedLineEdit::selectAll()
{ {
QTextCursor c = textCursor(); QTextCursor c = textCursor();
c.select(QTextCursor::LineUnderCursor); c.select(QTextCursor::LineUnderCursor);
setTextCursor(c); setTextCursor(c);
} }
void GroupedLineEdit::removeAllBlocks() void GroupedLineEdit::removeAllBlocks()
{ {
d->blocks.clear(); d->blocks.clear();
viewport()->update(); viewport()->update();
} }
QSize GroupedLineEdit::sizeHint() const QSize GroupedLineEdit::sizeHint() const
{ {
QSize rs( QSize rs(
40, 40,
document()->findBlock(0).layout()->lineAt(0).height() + document()->findBlock(0).layout()->lineAt(0).height() +
document()->documentMargin() * 2 + document()->documentMargin() * 2 +
frameWidth() * 2 frameWidth() * 2
); );
return rs; return rs;
} }
QSize GroupedLineEdit::minimumSizeHint() const QSize GroupedLineEdit::minimumSizeHint() const
{ {
return sizeHint(); return sizeHint();
} }
void GroupedLineEdit::keyPressEvent(QKeyEvent *e) void GroupedLineEdit::keyPressEvent(QKeyEvent *e)
{ {
switch (e->key()) { switch (e->key()) {
case Qt::Key_Return: case Qt::Key_Return:
case Qt::Key_Enter: case Qt::Key_Enter:
emit editingFinished(); emit editingFinished();
return; return;
} }
QPlainTextEdit::keyPressEvent(e);
QPlainTextEdit::keyPressEvent(e);
} }
void GroupedLineEdit::paintEvent(QPaintEvent *e) void GroupedLineEdit::paintEvent(QPaintEvent *e)
{ {
QTextLine line = document()->findBlock(0).layout()->lineAt(0); QTextLine line = document()->findBlock(0).layout()->lineAt(0);
QPainter painter(viewport()); QPainter painter(viewport());
painter.setRenderHint(QPainter::Antialiasing, true); painter.setRenderHint(QPainter::Antialiasing, true);
painter.setRenderHint(QPainter::HighQualityAntialiasing, true); painter.setRenderHint(QPainter::HighQualityAntialiasing, true);
painter.fillRect(0, 0, viewport()->width(), viewport()->height(), palette().base()); painter.fillRect(0, 0, viewport()->width(), viewport()->height(), palette().base());
QVectorIterator<QColor> i(d->colors); QVectorIterator<QColor> i(d->colors);
i.toFront(); i.toFront();
foreach (const Private::Block &block, d->blocks) { foreach (const Private::Block &block, d->blocks) {
qreal start_x = line.cursorToX(block.start, QTextLine::Trailing); qreal start_x = line.cursorToX(block.start, QTextLine::Trailing);
qreal end_x = line.cursorToX(block.end + 1, QTextLine::Leading); qreal end_x = line.cursorToX(block.end + 1, QTextLine::Leading);
QPainterPath path; QPainterPath path;
QRectF rectangle( QRectF rectangle(
start_x - 1.0 - double(horizontalScrollBar()->value()), start_x - 1.0 - double(horizontalScrollBar()->value()),
1.0, 1.0,
end_x - start_x + 2.0, end_x - start_x + 2.0,
double(viewport()->height() - 2) double(viewport()->height() - 2)
); );
if (! i.hasNext()) if (! i.hasNext())
i.toFront(); i.toFront();
path.addRoundedRect(rectangle, 5.0, 5.0); path.addRoundedRect(rectangle, 5.0, 5.0);
painter.setPen(i.peekNext()); painter.setPen(i.peekNext());
painter.setBrush(i.next().lighter(180)); painter.setBrush(i.next().lighter(180));
painter.drawPath(path); painter.drawPath(path);
} }
QPlainTextEdit::paintEvent(e); QPlainTextEdit::paintEvent(e);
} }

View file

@ -1,4 +1,11 @@
/* Original License: /*
* 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:
* *
* This file is part of the Nepomuk widgets collection * This file is part of the Nepomuk widgets collection
* Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr> * Copyright (c) 2013 Denis Steckelmacher <steckdenis@yahoo.fr>

View file

@ -840,6 +840,24 @@
<header>qt-ui/tagwidget.h</header> <header>qt-ui/tagwidget.h</header>
</customwidget> </customwidget>
</customwidgets> </customwidgets>
<tabstops>
<tabstop>scrollArea</tabstop>
<tabstop>dateTimeEdit</tabstop>
<tabstop>airtemp</tabstop>
<tabstop>watertemp</tabstop>
<tabstop>location</tabstop>
<tabstop>coordinates</tabstop>
<tabstop>divemaster</tabstop>
<tabstop>buddy</tabstop>
<tabstop>suit</tabstop>
<tabstop>tagWidget</tabstop>
<tabstop>notes</tabstop>
<tabstop>notesButtonBox</tabstop>
<tabstop>scrollArea_2</tabstop>
<tabstop>equipmentButtonBox</tabstop>
<tabstop>scrollArea_3</tabstop>
<tabstop>scrollArea_4</tabstop>
</tabstops>
<resources/> <resources/>
<connections/> <connections/>
</ui> </ui>

View file

@ -1,10 +1,12 @@
#include "tagwidget.h" #include "tagwidget.h"
#include <QPair> #include <QPair>
#include <QDebug> #include <QDebug>
#include <QAbstractItemView>
TagWidget::TagWidget(QWidget *parent) : GroupedLineEdit(parent), m_completer(NULL) TagWidget::TagWidget(QWidget *parent) : GroupedLineEdit(parent), m_completer(NULL)
{ {
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(reparse())); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(reparse()));
connect(this, SIGNAL(textChanged()), this, SLOT(reparse()));
addColor(QColor(0x00, 0xAE, 0xFF)); addColor(QColor(0x00, 0xAE, 0xFF));
addColor(QColor(0x00, 0x78, 0xB0)); addColor(QColor(0x00, 0x78, 0xB0));
@ -15,6 +17,7 @@ void TagWidget::setCompleter(QCompleter *completer)
m_completer = completer; m_completer = completer;
m_completer->setWidget(this); m_completer->setWidget(this);
connect(m_completer, SIGNAL(activated(QString)), this, SLOT(completionSelected(QString))); connect(m_completer, SIGNAL(activated(QString)), this, SLOT(completionSelected(QString)));
connect(m_completer, SIGNAL(highlighted(QString)), this, SLOT(completionSelected(QString)));
} }
QPair<int,int> TagWidget::getCursorTagPosition() { QPair<int,int> TagWidget::getCursorTagPosition() {
@ -98,7 +101,18 @@ void TagWidget::reparse()
currentText = ""; currentText = "";
if (m_completer) { if (m_completer) {
m_completer->setCompletionPrefix(currentText); m_completer->setCompletionPrefix(currentText);
m_completer->complete(); if (m_completer->completionCount() == 1) {
if (m_completer->currentCompletion() == currentText) {
QAbstractItemView *popup = m_completer->popup();
if (popup)
popup->hide();
}
else
m_completer->complete();
} else {
m_completer->complete();
}
} }
} }
@ -133,3 +147,21 @@ void TagWidget::clear() {
GroupedLineEdit::clear(); GroupedLineEdit::clear();
blockSignals(false); blockSignals(false);
} }
void TagWidget::keyPressEvent(QKeyEvent *e) {
switch (e->key()) {
case Qt::Key_Return:
case Qt::Key_Enter:
/*
* Fake the QLineEdit behaviour by simply
* closing the QAbstractViewitem
*/
if (m_completer) {
QAbstractItemView *popup = m_completer->popup();
if (popup)
popup->hide();
}
}
GroupedLineEdit::keyPressEvent(e);
}

View file

@ -19,6 +19,8 @@ public:
public slots: public slots:
void reparse(); void reparse();
void completionSelected(QString); void completionSelected(QString);
protected:
void keyPressEvent(QKeyEvent *e);
private: private:
QCompleter *m_completer; QCompleter *m_completer;
}; };