Navigation Menu

Skip to content

Commit

Permalink
Make highlightable line edit its own widget - QgsHighlightableLineEdit
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Mar 25, 2020
1 parent a2fb472 commit 9e4e32f
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 25 deletions.
54 changes: 54 additions & 0 deletions python/gui/auto_generated/qgshighlightablelineedit.sip.in
@@ -0,0 +1,54 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgshighlightablelineedit.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsHighlightableLineEdit: QgsFilterLineEdit
{
%Docstring

A QgsFilterLineEdit subclass with the ability to "highlight" the edges of the widget.

.. versionadded:: 3.14
%End

%TypeHeaderCode
#include "qgshighlightablelineedit.h"
%End
public:
QgsHighlightableLineEdit( QWidget *parent /TransferThis/ = 0 );

bool isHighlighted() const;
%Docstring
Returns ``True`` if the line edit is currently highlighted.

.. seealso:: :py:func:`setHighlighted`
%End

void setHighlighted( bool highlighted );
%Docstring
Sets whether the line edit is currently ``highlighted``.

.. seealso:: :py:func:`isHighlighted`
%End

protected:
virtual void paintEvent( QPaintEvent *e );


};


/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/qgshighlightablelineedit.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/gui/gui_auto.sip
Expand Up @@ -99,6 +99,7 @@
%Include auto_generated/qgsgui.sip
%Include auto_generated/qgshelp.sip
%Include auto_generated/qgshighlight.sip
%Include auto_generated/qgshighlightablelineedit.sip
%Include auto_generated/qgshistogramwidget.sip
%Include auto_generated/qgsidentifymenu.sip
%Include auto_generated/qgskeyvaluewidget.sip
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Expand Up @@ -406,6 +406,7 @@ SET(QGIS_GUI_SRCS
qgsguiutils.cpp
qgshighlight.cpp
qgshighlightablecombobox.cpp
qgshighlightablelineedit.cpp
qgshistogramwidget.cpp
qgshelp.cpp
qgsidentifymenu.cpp
Expand Down Expand Up @@ -626,6 +627,7 @@ SET(QGIS_GUI_HDRS
qgshelp.h
qgshighlight.h
qgshighlightablecombobox.h
qgshighlightablelineedit.h
qgshistogramwidget.h
qgsidentifymenu.h
qgsinstallgridshiftdialog.h
Expand Down
25 changes: 4 additions & 21 deletions src/gui/qgsfilewidget.cpp
Expand Up @@ -408,9 +408,8 @@ QString QgsFileWidget::toUrl( const QString &path ) const


QgsFileDropEdit::QgsFileDropEdit( QWidget *parent )
: QgsFilterLineEdit( parent )
: QgsHighlightableLineEdit( parent )
{
mDragActive = false;
setAcceptDrops( true );
}

Expand Down Expand Up @@ -510,8 +509,7 @@ void QgsFileDropEdit::dragEnterEvent( QDragEnterEvent *event )
if ( !filePath.isEmpty() )
{
event->acceptProposedAction();
mDragActive = true;
update();
setHighlighted( true );
}
else
{
Expand All @@ -523,8 +521,7 @@ void QgsFileDropEdit::dragLeaveEvent( QDragLeaveEvent *event )
{
QgsFilterLineEdit::dragLeaveEvent( event );
event->accept();
mDragActive = false;
update();
setHighlighted( false );
}

void QgsFileDropEdit::dropEvent( QDropEvent *event )
Expand All @@ -536,21 +533,7 @@ void QgsFileDropEdit::dropEvent( QDropEvent *event )
selectAll();
setFocus( Qt::MouseFocusReason );
event->acceptProposedAction();
mDragActive = false;
update();
}
}

void QgsFileDropEdit::paintEvent( QPaintEvent *e )
{
QgsFilterLineEdit::paintEvent( e );
if ( mDragActive )
{
QPainter p( this );
int width = 2; // width of highlight rectangle inside frame
p.setPen( QPen( palette().highlight(), width ) );
QRect r = rect().adjusted( width, width, -width, -width );
p.drawRect( r );
setHighlighted( false );
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/gui/qgsfilewidget.h
Expand Up @@ -26,7 +26,7 @@ class QHBoxLayout;

#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgsfilterlineedit.h"
#include "qgshighlightablelineedit.h"

/**
* \ingroup gui
Expand Down Expand Up @@ -243,7 +243,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
* or directories only. By default, dropping is limited to files only.
* \note not available in Python bindings
*/
class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit
class GUI_EXPORT QgsFileDropEdit: public QgsHighlightableLineEdit
{
Q_OBJECT

Expand All @@ -259,7 +259,6 @@ class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit
void dragEnterEvent( QDragEnterEvent *event ) override;
void dragLeaveEvent( QDragLeaveEvent *event ) override;
void dropEvent( QDropEvent *event ) override;
void paintEvent( QPaintEvent *e ) override;

private:

Expand All @@ -268,7 +267,6 @@ class GUI_EXPORT QgsFileDropEdit: public QgsFilterLineEdit

QStringList mAcceptableExtensions;
QgsFileWidget::StorageMode mStorageMode = QgsFileWidget::GetFile;
bool mDragActive;
friend class TestQgsFileWidget;
};

Expand Down
41 changes: 41 additions & 0 deletions src/gui/qgshighlightablelineedit.cpp
@@ -0,0 +1,41 @@
/***************************************************************************
qgshighlightablelineedit.h
-------------------------
Date : March 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/


#include "qgshighlightablelineedit.h"
#include <QPainter>

QgsHighlightableLineEdit::QgsHighlightableLineEdit( QWidget *parent )
: QgsFilterLineEdit( parent )
{}

void QgsHighlightableLineEdit::paintEvent( QPaintEvent *e )
{
QgsFilterLineEdit::paintEvent( e );
if ( mHighlight )
{
QPainter p( this );
int width = 2; // width of highlight rectangle inside frame
p.setPen( QPen( palette().highlight(), width ) );
QRect r = rect().adjusted( width, width, -width, -width );
p.drawRect( r );
}
}

void QgsHighlightableLineEdit::setHighlighted( bool highlighted )
{
mHighlight = highlighted;
update();
}
61 changes: 61 additions & 0 deletions src/gui/qgshighlightablelineedit.h
@@ -0,0 +1,61 @@
/***************************************************************************
qgshighlightablelineedit.h
-------------------------
Date : March 2020
Copyright : (C) 2020 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* 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. *
* *
***************************************************************************/

#ifndef QGSHIGHLIGHTABLELINEEDIT_H
#define QGSHIGHLIGHTABLELINEEDIT_H

#include <QWidget>

#include "qgis_gui.h"
#include "qgis_sip.h"
#include "qgsfilterlineedit.h"

/**
* \class QgsHighlightableLineEdit
* \ingroup gui
*
* A QgsFilterLineEdit subclass with the ability to "highlight" the edges of the widget.
*
* \since QGIS 3.14
*/
class GUI_EXPORT QgsHighlightableLineEdit: public QgsFilterLineEdit
{
Q_OBJECT

public:
QgsHighlightableLineEdit( QWidget *parent SIP_TRANSFERTHIS = nullptr );

/**
* Returns TRUE if the line edit is currently highlighted.
* \see setHighlighted()
*/
bool isHighlighted() const { return mHighlight; }

/**
* Sets whether the line edit is currently \a highlighted.
* \see isHighlighted()
*/
void setHighlighted( bool highlighted );

protected:
void paintEvent( QPaintEvent *e ) override;

private:

bool mHighlight = false;
};


#endif // QGSHIGHLIGHTABLELINEEDIT_H

0 comments on commit 9e4e32f

Please sign in to comment.