Commit 3f68e98f authored by Aurélien Gâteau's avatar Aurélien Gâteau

Add indicator overlay

parent 934fd4db
...@@ -44,7 +44,7 @@ add_subdirectory( doc ) ...@@ -44,7 +44,7 @@ add_subdirectory( doc )
########### next target ############### ########### 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) ki18n_wrap_ui(kruler_SRCS cfg_appearance.ui cfg_advanced.ui)
......
...@@ -54,7 +54,6 @@ ...@@ -54,7 +54,6 @@
#include "kruler.h" #include "kruler.h"
#include "krulersystemtray.h" #include "krulersystemtray.h"
#include "qautosizelabel.h"
#include "ui_cfg_appearance.h" #include "ui_cfg_appearance.h"
#include "ui_cfg_advanced.h" #include "ui_cfg_advanced.h"
...@@ -69,6 +68,14 @@ static const int MEDIUM2_TICK_SIZE = 15; ...@@ -69,6 +68,14 @@ static const int MEDIUM2_TICK_SIZE = 15;
static const int LARGE_TICK_SIZE = 18; static const int LARGE_TICK_SIZE = 18;
static const int THICKNESS = 70; 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 static const int CURSOR_SIZE = 15; // Must be an odd number
/** /**
...@@ -107,12 +114,6 @@ KLineal::KLineal( QWidget *parent ) ...@@ -107,12 +114,6 @@ KLineal::KLineal( QWidget *parent )
mRelativeScale = RulerSettings::self()->relativeScale(); mRelativeScale = RulerSettings::self()->relativeScale();
mAlwaysOnTopLayer = RulerSettings::self()->alwaysOnTop(); 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 ) { if ( mHorizontal ) {
resize( QSize( len, THICKNESS ) ); resize( QSize( len, THICKNESS ) );
} else { } else {
...@@ -184,7 +185,6 @@ KLineal::KLineal( QWidget *parent ) ...@@ -184,7 +185,6 @@ KLineal::KLineal( QWidget *parent )
: Qt::FramelessWindowHint ); : Qt::FramelessWindowHint );
setHorizontal( mHorizontal ); setHorizontal( mHorizontal );
adjustLabel();
} }
KLineal::~KLineal() KLineal::~KLineal()
...@@ -333,7 +333,6 @@ void KLineal::setHorizontal( bool horizontal ) ...@@ -333,7 +333,6 @@ void KLineal::setHorizontal( bool horizontal )
} }
setGeometry( r ); setGeometry( r );
adjustLabel();
updateScaleDirectionMenuItem(); updateScaleDirectionMenuItem();
...@@ -386,7 +385,6 @@ void KLineal::switchDirection() ...@@ -386,7 +385,6 @@ void KLineal::switchDirection()
mLeftToRight = !mLeftToRight; mLeftToRight = !mLeftToRight;
updateScaleDirectionMenuItem(); updateScaleDirectionMenuItem();
repaint(); repaint();
adjustLabel();
saveSettings(); saveSettings();
} }
...@@ -394,7 +392,6 @@ void KLineal::centerOrigin() ...@@ -394,7 +392,6 @@ void KLineal::centerOrigin()
{ {
mOffset = -( length() / 2 ); mOffset = -( length() / 2 );
repaint(); repaint();
adjustLabel();
saveSettings(); saveSettings();
} }
...@@ -408,7 +405,6 @@ void KLineal::slotOffset() ...@@ -408,7 +405,6 @@ void KLineal::slotOffset()
if ( ok ) { if ( ok ) {
mOffset = newOffset; mOffset = newOffset;
repaint(); repaint();
adjustLabel();
saveSettings(); saveSettings();
} }
} }
...@@ -485,7 +481,6 @@ void KLineal::switchRelativeScale( bool checked ) ...@@ -485,7 +481,6 @@ void KLineal::switchRelativeScale( bool checked )
mOffsetAction->setEnabled( !mRelativeScale ); mOffsetAction->setEnabled( !mRelativeScale );
repaint(); repaint();
adjustLabel();
saveSettings(); saveSettings();
} }
...@@ -531,41 +526,20 @@ QPoint KLineal::localCursorPos() const ...@@ -531,41 +526,20 @@ QPoint KLineal::localCursorPos() const
return QCursor::pos() - pos(); return QCursor::pos() - pos();
} }
/** QString KLineal::indicatorText() const
* updates the current value label
*/
void KLineal::adjustLabel()
{ {
if ( isResizing() || !underMouse() ) { int xy = mHorizontal ? localCursorPos().x() : localCursorPos().y();
mLabel->hide();
update();
return;
}
QString text;
int len = mHorizontal ? localCursorPos().x() : localCursorPos().y();
if ( !mRelativeScale ) { if ( !mRelativeScale ) {
if ( !mLeftToRight ) { int len = mLeftToRight ? xy + 1 : length() - xy;
len = length() - len; return i18n( "%1 px", len );
}
text = i18n( "%1 px", len );
} else { } else {
len = ( len * 100.f ) / length(); int len = ( xy * 100.f ) / length();
if ( !mLeftToRight ) { if ( !mLeftToRight ) {
len = 100 - len; 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 ) void KLineal::keyPressEvent( QKeyEvent *e )
...@@ -609,7 +583,7 @@ void KLineal::keyPressEvent( QKeyEvent *e ) ...@@ -609,7 +583,7 @@ void KLineal::keyPressEvent( QKeyEvent *e )
void KLineal::leaveEvent( QEvent *e ) void KLineal::leaveEvent( QEvent *e )
{ {
Q_UNUSED( e ); Q_UNUSED( e );
adjustLabel(); update();
} }
void KLineal::mouseMoveEvent( QMouseEvent *inEvent ) void KLineal::mouseMoveEvent( QMouseEvent *inEvent )
...@@ -630,14 +604,12 @@ void KLineal::mouseMoveEvent( QMouseEvent *inEvent ) ...@@ -630,14 +604,12 @@ void KLineal::mouseMoveEvent( QMouseEvent *inEvent )
r.setTop( QCursor::pos().y() - mDragOffset.y() ); r.setTop( QCursor::pos().y() - mDragOffset.y() );
} }
setGeometry( r ); setGeometry( r );
adjustLabel();
} else if ( mRulerState == StateEnd ) { } else if ( mRulerState == StateEnd ) {
QPoint end = QCursor::pos() + mDragOffset - pos(); QPoint end = QCursor::pos() + mDragOffset - pos();
QSize size = mHorizontal QSize size = mHorizontal
? QSize( end.x(), height() ) ? QSize( end.x(), height() )
: QSize( width(), end.y() ); : QSize( width(), end.y() );
resize( size ); resize( size );
adjustLabel();
} }
} else { } else {
QPoint cpos = localCursorPos(); QPoint cpos = localCursorPos();
...@@ -647,7 +619,7 @@ void KLineal::mouseMoveEvent( QMouseEvent *inEvent ) ...@@ -647,7 +619,7 @@ void KLineal::mouseMoveEvent( QMouseEvent *inEvent )
} else { } else {
setCursor( mCrossCursor ); setCursor( mCrossCursor );
} }
adjustLabel(); update();
} }
} }
...@@ -678,7 +650,6 @@ void KLineal::mousePressEvent( QMouseEvent *inEvent ) ...@@ -678,7 +650,6 @@ void KLineal::mousePressEvent( QMouseEvent *inEvent )
} }
} }
} }
adjustLabel();
} else if ( inEvent->button() == Qt::MidButton ) { } else if ( inEvent->button() == Qt::MidButton ) {
mClicked = true; mClicked = true;
rotate(); rotate();
...@@ -734,7 +705,6 @@ void KLineal::mouseReleaseEvent( QMouseEvent *inEvent ) ...@@ -734,7 +705,6 @@ void KLineal::mouseReleaseEvent( QMouseEvent *inEvent )
} else if ( nativeMove() ) { } else if ( nativeMove() ) {
stopNativeMove( inEvent ); stopNativeMove( inEvent );
} }
adjustLabel();
} }
void KLineal::wheelEvent( QWheelEvent *e ) void KLineal::wheelEvent( QWheelEvent *e )
...@@ -745,11 +715,9 @@ void KLineal::wheelEvent( QWheelEvent *e ) ...@@ -745,11 +715,9 @@ void KLineal::wheelEvent( QWheelEvent *e )
// changing offset // changing offset
if ( e->buttons() == Qt::LeftButton ) { if ( e->buttons() == Qt::LeftButton ) {
if ( !mRelativeScale ) { if ( !mRelativeScale ) {
mLabel->show();
mOffset += numSteps; mOffset += numSteps;
repaint(); repaint();
mLabel->setText( i18n( "Offset: %1", mOffset ) );
saveSettings(); saveSettings();
} }
} }
...@@ -765,7 +733,7 @@ void KLineal::drawScale( QPainter &painter ) ...@@ -765,7 +733,7 @@ void KLineal::drawScale( QPainter &painter )
painter.setPen( Qt::black ); painter.setPen( Qt::black );
QFont font = mScaleFont; QFont font = mScaleFont;
painter.setFont( font ); painter.setFont( font );
int longLen = mHorizontal ? width() : height(); int longLen = length();
if ( !mRelativeScale ) { if ( !mRelativeScale ) {
int digit; int digit;
...@@ -839,9 +807,13 @@ void KLineal::drawScaleTick( QPainter &painter, int x, int len ) ...@@ -839,9 +807,13 @@ void KLineal::drawScaleTick( QPainter &painter, int x, int len )
{ {
int w = width(); int w = width();
int h = height(); 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. // indicator is at position 0 it measures a length of 1 pixel.
--x; if ( mLeftToRight ) {
--x;
} else {
++x;
}
if ( mHorizontal ) { if ( mHorizontal ) {
painter.drawLine( x, 0, x, len ); painter.drawLine( x, 0, x, len );
painter.drawLine( x, h, x, h - len ); painter.drawLine( x, h, x, h - len );
...@@ -881,6 +853,65 @@ void KLineal::drawResizeHandle( QPainter &painter, Qt::Edge edge ) ...@@ -881,6 +853,65 @@ void KLineal::drawResizeHandle( QPainter &painter, Qt::Edge edge )
painter.setOpacity( 1 ); 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 * actually draws the ruler
*/ */
...@@ -895,8 +926,8 @@ void KLineal::paintEvent(QPaintEvent *inEvent ) ...@@ -895,8 +926,8 @@ void KLineal::paintEvent(QPaintEvent *inEvent )
drawResizeHandle( painter, mHorizontal ? Qt::LeftEdge : Qt::TopEdge ); drawResizeHandle( painter, mHorizontal ? Qt::LeftEdge : Qt::TopEdge );
drawResizeHandle( painter, mHorizontal ? Qt::RightEdge : Qt::BottomEdge ); drawResizeHandle( painter, mHorizontal ? Qt::RightEdge : Qt::BottomEdge );
if ( underMouse() && !isResizing() ) { if ( underMouse() && !isResizing() ) {
painter.setPen( Qt::red ); int xy = mHorizontal ? localCursorPos().x() : localCursorPos().y();
QPoint pos = localCursorPos(); drawIndicatorOverlay( painter, xy );
drawScaleTick( painter, mHorizontal ? pos.x() : pos.y(), LARGE_TICK_SIZE ); drawIndicatorText( painter, xy );
} }
} }
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include <QWidget> #include <QWidget>
class QAction; class QAction;
class QAutoSizeLabel;
class QIcon; class QIcon;
class QMenu; class QMenu;
class QToolButton; class QToolButton;
...@@ -63,6 +62,8 @@ private: ...@@ -63,6 +62,8 @@ private:
void drawScaleText( QPainter &painter, int x, const QString &text ); void drawScaleText( QPainter &painter, int x, const QString &text );
void drawScaleTick( QPainter &painter, int x, int length ); void drawScaleTick( QPainter &painter, int x, int length );
void drawResizeHandle( QPainter &painter, Qt::Edge edge ); void drawResizeHandle( QPainter &painter, Qt::Edge edge );
void drawIndicatorOverlay( QPainter &painter, int xy );
void drawIndicatorText( QPainter &painter, int xy );
void updateScaleDirectionMenuItem(); void updateScaleDirectionMenuItem();
QRect beginRect() const; QRect beginRect() const;
...@@ -72,6 +73,7 @@ private: ...@@ -72,6 +73,7 @@ private:
bool nativeMove() const; bool nativeMove() const;
void startNativeMove( QMouseEvent *e ); void startNativeMove( QMouseEvent *e );
void stopNativeMove( QMouseEvent *e ); void stopNativeMove( QMouseEvent *e );
QString indicatorText() const;
enum RulerState { enum RulerState {
StateNone, StateNone,
...@@ -83,7 +85,6 @@ private: ...@@ -83,7 +85,6 @@ private:
RulerState mRulerState; RulerState mRulerState;
QPoint mLastClickPos; QPoint mLastClickPos;
QPoint mDragOffset; QPoint mDragOffset;
QAutoSizeLabel *mLabel;
bool mHorizontal; bool mHorizontal;
QMenu *mMenu; QMenu *mMenu;
QAction *mCloseAction; QAction *mCloseAction;
...@@ -110,7 +111,6 @@ private: ...@@ -110,7 +111,6 @@ private:
public slots: public slots:
void rotate(); void rotate();
void showMenu(); void showMenu();
void adjustLabel();
void switchDirection(); void switchDirection();
void centerOrigin(); void centerOrigin();
void slotOffset(); void slotOffset();
......
////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2008 Mathias Soeken <msoeken@informatik.uni-bremen.de> //
// //
// 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() ) );
}
////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2008 Mathias Soeken <msoeken@informatik.uni-bremen.de> //
// //
// 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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment