summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAurélien Gâteau <[email protected]>2017-06-14 19:33:37 +0200
committerAurélien Gâteau <[email protected]>2017-06-14 19:33:37 +0200
commit3f68e98f6f5a9328aaf44829238f19e81f61fb74 (patch)
treec5be91a744c9317f0803173d8b753d8b847ac262
parent934fd4db85ec0430189cc55bdf23518765872369 (diff)
Add indicator overlay
-rw-r--r--CMakeLists.txt2
-rw-r--r--klineal.cpp139
-rw-r--r--klineal.h6
-rw-r--r--qautosizelabel.cpp53
-rw-r--r--qautosizelabel.h41
5 files changed, 89 insertions, 152 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 1df3fe0..e8953bd 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -44,7 +44,7 @@ add_subdirectory( doc )
########### next target ###############
-set(kruler_SRCS qautosizelabel.cpp klineal.cpp main.cpp krulersystemtray.cpp)
+set(kruler_SRCS klineal.cpp main.cpp krulersystemtray.cpp)
ki18n_wrap_ui(kruler_SRCS cfg_appearance.ui cfg_advanced.ui)
diff --git a/klineal.cpp b/klineal.cpp
index 07f34f9..47120ad 100644
--- a/klineal.cpp
+++ b/klineal.cpp
@@ -54,7 +54,6 @@
#include "kruler.h"
#include "krulersystemtray.h"
-#include "qautosizelabel.h"
#include "ui_cfg_appearance.h"
#include "ui_cfg_advanced.h"
@@ -69,6 +68,14 @@ static const int MEDIUM2_TICK_SIZE = 15;
static const int LARGE_TICK_SIZE = 18;
static const int THICKNESS = 70;
+
+static const qreal OVERLAY_OPACITY = 0.1;
+static const qreal OVERLAY_BORDER_OPACITY = 0.3;
+
+static const int INDICATOR_MARGIN = 6;
+static const int INDICATOR_RECT_RADIUS = 3;
+static const qreal INDICATOR_RECT_OPACITY = 0.6;
+
static const int CURSOR_SIZE = 15; // Must be an odd number
/**
@@ -107,12 +114,6 @@ KLineal::KLineal( QWidget *parent )
mRelativeScale = RulerSettings::self()->relativeScale();
mAlwaysOnTopLayer = RulerSettings::self()->alwaysOnTop();
- mLabel = new QAutoSizeLabel( this );
- mLabel->setWhatsThis( i18n( "This is the current distance measured in pixels." ) );
- QPalette pal = mLabel->palette();
- pal.setColor( QPalette::WindowText, Qt::red );
- mLabel->setPalette( pal );
-
if ( mHorizontal ) {
resize( QSize( len, THICKNESS ) );
} else {
@@ -184,7 +185,6 @@ KLineal::KLineal( QWidget *parent )
: Qt::FramelessWindowHint );
setHorizontal( mHorizontal );
- adjustLabel();
}
KLineal::~KLineal()
@@ -333,7 +333,6 @@ void KLineal::setHorizontal( bool horizontal )
}
setGeometry( r );
- adjustLabel();
updateScaleDirectionMenuItem();
@@ -386,7 +385,6 @@ void KLineal::switchDirection()
mLeftToRight = !mLeftToRight;
updateScaleDirectionMenuItem();
repaint();
- adjustLabel();
saveSettings();
}
@@ -394,7 +392,6 @@ void KLineal::centerOrigin()
{
mOffset = -( length() / 2 );
repaint();
- adjustLabel();
saveSettings();
}
@@ -408,7 +405,6 @@ void KLineal::slotOffset()
if ( ok ) {
mOffset = newOffset;
repaint();
- adjustLabel();
saveSettings();
}
}
@@ -485,7 +481,6 @@ void KLineal::switchRelativeScale( bool checked )
mOffsetAction->setEnabled( !mRelativeScale );
repaint();
- adjustLabel();
saveSettings();
}
@@ -531,41 +526,20 @@ QPoint KLineal::localCursorPos() const
return QCursor::pos() - pos();
}
-/**
- * updates the current value label
- */
-void KLineal::adjustLabel()
+QString KLineal::indicatorText() const
{
- if ( isResizing() || !underMouse() ) {
- mLabel->hide();
- update();
- return;
- }
-
- QString text;
- int len = mHorizontal ? localCursorPos().x() : localCursorPos().y();
+ int xy = mHorizontal ? localCursorPos().x() : localCursorPos().y();
if ( !mRelativeScale ) {
- if ( !mLeftToRight ) {
- len = length() - len;
- }
- text = i18n( "%1 px", len );
+ int len = mLeftToRight ? xy + 1 : length() - xy;
+ return i18n( "%1 px", len );
} else {
- len = ( len * 100.f ) / length();
+ int len = ( xy * 100.f ) / length();
if ( !mLeftToRight ) {
len = 100 - len;
}
- text = i18n( "%1%", len );
+ return i18n( "%1%", len );
}
- mLabel->setText( text );
-
- QFontMetrics fm = mLabel->fontMetrics();
- QPoint pos = mHorizontal
- ? QPoint( height() / 2, ( height() - fm.ascent() ) / 2 )
- : QPoint( ( width() - mLabel->width() ) / 2, width() / 2 );
- mLabel->move( pos );
- mLabel->show();
- update();
}
void KLineal::keyPressEvent( QKeyEvent *e )
@@ -609,7 +583,7 @@ void KLineal::keyPressEvent( QKeyEvent *e )
void KLineal::leaveEvent( QEvent *e )
{
Q_UNUSED( e );
- adjustLabel();
+ update();
}
void KLineal::mouseMoveEvent( QMouseEvent *inEvent )
@@ -630,14 +604,12 @@ void KLineal::mouseMoveEvent( QMouseEvent *inEvent )
r.setTop( QCursor::pos().y() - mDragOffset.y() );
}
setGeometry( r );
- adjustLabel();
} else if ( mRulerState == StateEnd ) {
QPoint end = QCursor::pos() + mDragOffset - pos();
QSize size = mHorizontal
? QSize( end.x(), height() )
: QSize( width(), end.y() );
resize( size );
- adjustLabel();
}
} else {
QPoint cpos = localCursorPos();
@@ -647,7 +619,7 @@ void KLineal::mouseMoveEvent( QMouseEvent *inEvent )
} else {
setCursor( mCrossCursor );
}
- adjustLabel();
+ update();
}
}
@@ -678,7 +650,6 @@ void KLineal::mousePressEvent( QMouseEvent *inEvent )
}
}
}
- adjustLabel();
} else if ( inEvent->button() == Qt::MidButton ) {
mClicked = true;
rotate();
@@ -734,7 +705,6 @@ void KLineal::mouseReleaseEvent( QMouseEvent *inEvent )
} else if ( nativeMove() ) {
stopNativeMove( inEvent );
}
- adjustLabel();
}
void KLineal::wheelEvent( QWheelEvent *e )
@@ -745,11 +715,9 @@ void KLineal::wheelEvent( QWheelEvent *e )
// changing offset
if ( e->buttons() == Qt::LeftButton ) {
if ( !mRelativeScale ) {
- mLabel->show();
mOffset += numSteps;
repaint();
- mLabel->setText( i18n( "Offset: %1", mOffset ) );
saveSettings();
}
}
@@ -765,7 +733,7 @@ void KLineal::drawScale( QPainter &painter )
painter.setPen( Qt::black );
QFont font = mScaleFont;
painter.setFont( font );
- int longLen = mHorizontal ? width() : height();
+ int longLen = length();
if ( !mRelativeScale ) {
int digit;
@@ -839,9 +807,13 @@ void KLineal::drawScaleTick( QPainter &painter, int x, int len )
{
int w = width();
int h = height();
- // Offset by one because we are measuring lengths, not position, so when the
+ // Offset by one because we are measuring lengths, not positions, so when the
// indicator is at position 0 it measures a length of 1 pixel.
- --x;
+ if ( mLeftToRight ) {
+ --x;
+ } else {
+ ++x;
+ }
if ( mHorizontal ) {
painter.drawLine( x, 0, x, len );
painter.drawLine( x, h, x, h - len );
@@ -881,6 +853,65 @@ void KLineal::drawResizeHandle( QPainter &painter, Qt::Edge edge )
painter.setOpacity( 1 );
}
+void KLineal::drawIndicatorOverlay( QPainter &painter, int xy )
+{
+ painter.setPen( Qt::red );
+ painter.setOpacity( OVERLAY_OPACITY );
+ if ( mHorizontal ) {
+ QPointF p1( mLeftToRight ? 0 : width(), 0 );
+ QPointF p2( xy, THICKNESS );
+ QRectF rect( p1, p2 );
+ painter.fillRect( rect, Qt::red );
+
+ painter.setOpacity( OVERLAY_BORDER_OPACITY );
+ painter.drawLine( xy, 0, xy, THICKNESS );
+ } else {
+ QPointF p1( 0, mLeftToRight ? 0 : height() );
+ QPointF p2( THICKNESS, xy );
+ QRectF rect( p1, p2 );
+ painter.fillRect( rect, Qt::red );
+
+ painter.setOpacity( OVERLAY_BORDER_OPACITY );
+ painter.drawLine( 0, xy, THICKNESS, xy );
+ }
+}
+
+void KLineal::drawIndicatorText( QPainter &painter, int xy )
+{
+ QString text = indicatorText();
+ painter.setFont( font() );
+ QFontMetrics fm = QFontMetrics( font() );
+ int tx, ty;
+ int tw = fm.width( text );
+ if ( mHorizontal ) {
+ tx = xy + INDICATOR_MARGIN;
+ if ( tx + tw > width() ) {
+ tx = xy - tw - INDICATOR_MARGIN;
+ }
+ ty = height() - SMALL_TICK_SIZE - INDICATOR_RECT_RADIUS;
+ } else {
+ tx = ( width() - tw ) / 2;
+ ty = xy + fm.ascent() + INDICATOR_MARGIN;
+ if ( ty > height() ) {
+ ty = xy - INDICATOR_MARGIN;
+ }
+ }
+
+ // Draw background rect
+ painter.setRenderHint( QPainter::Antialiasing );
+ painter.setOpacity( INDICATOR_RECT_OPACITY );
+ painter.setBrush( Qt::red );
+ QRectF bgRect( tx, ty - fm.ascent() + 1, tw, fm.ascent() );
+ bgRect.adjust( -INDICATOR_RECT_RADIUS, -INDICATOR_RECT_RADIUS, INDICATOR_RECT_RADIUS, INDICATOR_RECT_RADIUS );
+ bgRect.translate( 0.5, 0.5 );
+ painter.drawRoundedRect( bgRect, INDICATOR_RECT_RADIUS, INDICATOR_RECT_RADIUS );
+
+ // Draw text
+ painter.setOpacity( 1 );
+ painter.setPen( Qt::white );
+ painter.drawText( tx, ty, text );
+}
+
/**
* actually draws the ruler
*/
@@ -895,8 +926,8 @@ void KLineal::paintEvent(QPaintEvent *inEvent )
drawResizeHandle( painter, mHorizontal ? Qt::LeftEdge : Qt::TopEdge );
drawResizeHandle( painter, mHorizontal ? Qt::RightEdge : Qt::BottomEdge );
if ( underMouse() && !isResizing() ) {
- painter.setPen( Qt::red );
- QPoint pos = localCursorPos();
- drawScaleTick( painter, mHorizontal ? pos.x() : pos.y(), LARGE_TICK_SIZE );
+ int xy = mHorizontal ? localCursorPos().x() : localCursorPos().y();
+ drawIndicatorOverlay( painter, xy );
+ drawIndicatorText( painter, xy );
}
}
diff --git a/klineal.h b/klineal.h
index f0f338a..1b04e21 100644
--- a/klineal.h
+++ b/klineal.h
@@ -21,7 +21,6 @@
#include <QWidget>
class QAction;
-class QAutoSizeLabel;
class QIcon;
class QMenu;
class QToolButton;
@@ -63,6 +62,8 @@ private:
void drawScaleText( QPainter &painter, int x, const QString &text );
void drawScaleTick( QPainter &painter, int x, int length );
void drawResizeHandle( QPainter &painter, Qt::Edge edge );
+ void drawIndicatorOverlay( QPainter &painter, int xy );
+ void drawIndicatorText( QPainter &painter, int xy );
void updateScaleDirectionMenuItem();
QRect beginRect() const;
@@ -72,6 +73,7 @@ private:
bool nativeMove() const;
void startNativeMove( QMouseEvent *e );
void stopNativeMove( QMouseEvent *e );
+ QString indicatorText() const;
enum RulerState {
StateNone,
@@ -83,7 +85,6 @@ private:
RulerState mRulerState;
QPoint mLastClickPos;
QPoint mDragOffset;
- QAutoSizeLabel *mLabel;
bool mHorizontal;
QMenu *mMenu;
QAction *mCloseAction;
@@ -110,7 +111,6 @@ private:
public slots:
void rotate();
void showMenu();
- void adjustLabel();
void switchDirection();
void centerOrigin();
void slotOffset();
diff --git a/qautosizelabel.cpp b/qautosizelabel.cpp
deleted file mode 100644
index 2b98349..0000000
--- a/qautosizelabel.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-////////////////////////////////////////////////////////////////////////////
-// //
-// Copyright (C) 2008 Mathias Soeken <[email protected]> //
-// //
-// This program is free software; you can redistribute it and/or //
-// modify it under the terms of the GNU General Public License //
-// as published by the Free Software Foundation; either version 2 //
-// of the License, or (at your option) any later version. //
-// //
-// This program 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 General Public License for more details. //
-// //
-// You should have received a copy of the GNU General Public License //
-// along with this program; if not, write to the Free Software //
-// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
-// 02110-1301, USA. //
-////////////////////////////////////////////////////////////////////////////
-
-#include "qautosizelabel.h"
-
-#include <QFontMetrics>
-
-QAutoSizeLabel::QAutoSizeLabel( QWidget *parent, Qt::WindowFlags f )
- : QLabel( parent, f )
-{
- resize();
-}
-
-QAutoSizeLabel::QAutoSizeLabel( const QString &text, QWidget *parent, Qt::WindowFlags f )
- : QLabel( text, parent, f )
-{
- resize();
-}
-
-QAutoSizeLabel::~QAutoSizeLabel()
-{
-}
-
-void QAutoSizeLabel::setText( const QString &text )
-{
- QLabel::setText( text );
- resize();
-}
-
-void QAutoSizeLabel::resize()
-{
- QFontMetrics fm( font(), this );
- QLabel::resize( fm.size( Qt::TextSingleLine, text() ) );
-}
-
-
diff --git a/qautosizelabel.h b/qautosizelabel.h
deleted file mode 100644
index ce65470..0000000
--- a/qautosizelabel.h
+++ /dev/null
@@ -1,41 +0,0 @@
-////////////////////////////////////////////////////////////////////////////
-// //
-// Copyright (C) 2008 Mathias Soeken <[email protected]> //
-// //
-// This program is free software; you can redistribute it and/or //
-// modify it under the terms of the GNU General Public License //
-// as published by the Free Software Foundation; either version 2 //
-// of the License, or (at your option) any later version. //
-// //
-// This program 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 General Public License for more details. //
-// //
-// You should have received a copy of the GNU General Public License //
-// along with this program; if not, write to the Free Software //
-// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA //
-// 02110-1301, USA. //
-////////////////////////////////////////////////////////////////////////////
-
-#ifndef QAUTOSIZELABEL_H
-#define QAUTOSIZELABEL_H
-
-#include <QLabel>
-
-class QAutoSizeLabel : public QLabel {
- Q_OBJECT
-
- public:
- explicit QAutoSizeLabel( QWidget *parent = 0, Qt::WindowFlags f = 0 );
- explicit QAutoSizeLabel( const QString &text, QWidget *parent = 0, Qt::WindowFlags f = 0 );
- virtual ~QAutoSizeLabel();
-
- public Q_SLOTS:
- void setText( const QString &text );
-
- private:
- void resize();
-};
-
-#endif