forked from pencil2d/pencil
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement double-click to edit layer name
- Loading branch information
Showing
5 changed files
with
172 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "lineeditwidget.h" | ||
|
||
#include <QLineEdit> | ||
#include <QEvent> | ||
#include <QKeyEvent> | ||
|
||
LineEditWidget::LineEditWidget(QWidget* parent, QString text) | ||
: QLineEdit(parent) | ||
{ | ||
setObjectName("LineEditWidget"); | ||
|
||
setStyleSheet("LineEditWidget[readOnly=true] {" | ||
"background-color: transparent;" | ||
"border: 0;" | ||
"}"); | ||
setText(text); | ||
setReadOnly(true); | ||
} | ||
|
||
void LineEditWidget::mousePressEvent(QMouseEvent* event) | ||
{ | ||
QLineEdit::mousePressEvent(event); | ||
|
||
// The event is ignored explicitly | ||
// so that we can allow it to propergate up the chain | ||
event->ignore(); | ||
} | ||
|
||
void LineEditWidget::mouseMoveEvent(QMouseEvent* event) | ||
{ | ||
QLineEdit::mouseMoveEvent(event); | ||
|
||
// The event is ignored explicitly | ||
// so that we can allow it to propergate up the chain | ||
event->ignore(); | ||
} | ||
|
||
void LineEditWidget::mouseReleaseEvent(QMouseEvent* event) | ||
{ | ||
QLineEdit::mouseReleaseEvent(event); | ||
|
||
// The event is ignored explicitly | ||
// so that we can allow it to propergate up the chain | ||
event->ignore(); | ||
} | ||
|
||
void LineEditWidget::mouseDoubleClickEvent(QMouseEvent* event) | ||
{ | ||
QLineEdit::mouseDoubleClickEvent(event); | ||
|
||
setReadOnly(false); | ||
setFocus(); | ||
selectAll(); | ||
|
||
reloadStylesheet(); | ||
|
||
// There's no ignore event here because in this case we want to catch it. | ||
} | ||
|
||
void LineEditWidget::focusOutEvent(QFocusEvent *event) | ||
{ | ||
QLineEdit::focusOutEvent(event); | ||
setReadOnly(true); | ||
reloadStylesheet(); | ||
} | ||
|
||
void LineEditWidget::reloadStylesheet() | ||
{ | ||
// Apparently it's good enough to just call setStyleSheet with its current styling | ||
// to make it update. | ||
setStyleSheet(styleSheet()); | ||
} | ||
|
||
void LineEditWidget::keyPressEvent(QKeyEvent* event) | ||
{ | ||
QLineEdit::keyPressEvent(event); | ||
|
||
if (event->key() == Qt::Key_Return) { | ||
setReadOnly(true); | ||
} else if (event->key() == Qt::Key_Escape) { | ||
undo(); | ||
setReadOnly(true); | ||
} | ||
reloadStylesheet(); | ||
} | ||
|
||
void LineEditWidget::deselect() | ||
{ | ||
QLineEdit::deselect(); | ||
setReadOnly(true); | ||
|
||
reloadStylesheet(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef LINEEDITWIDGET_H | ||
#define LINEEDITWIDGET_H | ||
|
||
#include <QObject> | ||
#include <QLineEdit> | ||
|
||
class Layer; | ||
class QLabel; | ||
class QLineEdit; | ||
class QStackedLayout; | ||
|
||
class LineEditWidget : public QLineEdit | ||
{ | ||
Q_OBJECT | ||
public: | ||
LineEditWidget(QWidget* parent, QString text); | ||
|
||
void mousePressEvent(QMouseEvent*) override; | ||
void mouseMoveEvent(QMouseEvent* event) override; | ||
void mouseReleaseEvent(QMouseEvent* event) override; | ||
void mouseDoubleClickEvent(QMouseEvent *) override; | ||
void focusOutEvent(QFocusEvent* event) override; | ||
void keyPressEvent(QKeyEvent* event) override; | ||
|
||
void deselect(); | ||
|
||
private: | ||
// The stylesheet has to be updated on every event | ||
// where the read-only property is changed | ||
void reloadStylesheet(); | ||
|
||
}; | ||
|
||
#endif // LINEEDITWIDGET_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters