1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-24 04:36:34 +01:00

LineEdit: Cleanup text edit actions code

This commit is contained in:
David Rosca 2014-10-18 16:33:37 +02:00
parent ada212bc59
commit 2c0582b9a1
6 changed files with 63 additions and 80 deletions

View File

@ -115,6 +115,10 @@ void LineEdit::init()
pasteAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
connect(pasteAction, SIGNAL(triggered()), SLOT(paste()));
QAction* pasteAndGoAction = new QAction(this);
pasteAndGoAction->setShortcut(QKeySequence(QSL("Ctrl+Shift+V")));
pasteAndGoAction->setShortcutContext(Qt::WidgetWithChildrenShortcut);
QAction* deleteAction = new QAction(QIcon::fromTheme(QSL("edit-delete")), tr("Delete"), this);
connect(deleteAction, SIGNAL(triggered()), SLOT(slotDelete()));
@ -131,6 +135,7 @@ void LineEdit::init()
m_editActions[Cut] = cutAction;
m_editActions[Copy] = copyAction;
m_editActions[Paste] = pasteAction;
m_editActions[PasteAndGo] = pasteAndGoAction;
m_editActions[Delete] = deleteAction;
m_editActions[ClearAll] = clearAllAction;
m_editActions[SelectAll] = selectAllAction;
@ -141,9 +146,17 @@ void LineEdit::init()
addAction(cutAction);
addAction(copyAction);
addAction(pasteAction);
addAction(pasteAndGoAction);
addAction(deleteAction);
addAction(clearAllAction);
addAction(selectAllAction);
// Connections to update edit actions
connect(this, SIGNAL(textChanged(QString)), this, SLOT(updateActions()));
connect(this, SIGNAL(selectionChanged()), this, SLOT(updateActions()));
connect(QApplication::clipboard(), SIGNAL(dataChanged()), this, SLOT(updateActions()));
updateActions();
}
bool LineEdit::event(QEvent* event)
@ -167,62 +180,32 @@ bool LineEdit::event(QEvent* event)
#define ACCEL_KEY(k) QLatin1Char('\t') + QKeySequence(k).toString()
// Modified QLineEdit::createStandardContextMenu to support icons and PasteAndGo action
QMenu* LineEdit::createContextMenu(QAction* pasteAndGoAction)
QMenu* LineEdit::createContextMenu()
{
QMenu* popup = new QMenu(this);
popup->setObjectName(QSL("qt_edit_menu"));
QAction* action = 0;
if (!isReadOnly()) {
action = popup->addAction(QIcon::fromTheme(QSL("edit-undo")), tr("&Undo") + ACCEL_KEY(QKeySequence::Undo));
action->setEnabled(isUndoAvailable());
connect(action, SIGNAL(triggered()), SLOT(undo()));
action = popup->addAction(QIcon::fromTheme(QSL("edit-redo")), tr("&Redo") + ACCEL_KEY(QKeySequence::Redo));
action->setEnabled(isRedoAvailable());
connect(action, SIGNAL(triggered()), SLOT(redo()));
popup->addAction(m_editActions[Undo]);
popup->addAction(m_editActions[Redo]);
popup->addSeparator();
popup->addAction(m_editActions[Cut]);
}
#ifndef QT_NO_CLIPBOARD
if (!isReadOnly()) {
action = popup->addAction(QIcon::fromTheme(QSL("edit-cut")), tr("Cu&t") + ACCEL_KEY(QKeySequence::Cut));
action->setEnabled(hasSelectedText() && echoMode() == QLineEdit::Normal);
connect(action, SIGNAL(triggered()), SLOT(cut()));
}
action = popup->addAction(QIcon::fromTheme(QSL("edit-copy")), tr("&Copy") + ACCEL_KEY(QKeySequence::Copy));
action->setEnabled(hasSelectedText() && echoMode() == QLineEdit::Normal);
connect(action, SIGNAL(triggered()), SLOT(copy()));
popup->addAction(m_editActions[Copy]);
if (!isReadOnly()) {
action = popup->addAction(QIcon::fromTheme(QSL("edit-paste")), tr("&Paste") + ACCEL_KEY(QKeySequence::Paste));
action->setEnabled(!QApplication::clipboard()->text().isEmpty());
connect(action, SIGNAL(triggered()), SLOT(paste()));
popup->addAction(m_editActions[Paste]);
pasteAndGoAction->setEnabled(action->isEnabled());
popup->addAction(pasteAndGoAction);
}
#endif
if (!m_editActions[PasteAndGo]->text().isEmpty())
popup->addAction(m_editActions[PasteAndGo]);
if (!isReadOnly()) {
action = popup->addAction(QIcon::fromTheme(QSL("edit-delete")), tr("Delete") + ACCEL_KEY(QKeySequence::Delete));
action->setEnabled(hasSelectedText());
connect(action, SIGNAL(triggered()), this, SLOT(slotDelete()));
action = popup->addAction(QIcon::fromTheme(QSL("edit-clear")), tr("Clear All"));
connect(action, SIGNAL(triggered()), this, SLOT(clear()));
popup->addAction(m_editActions[Delete]);
popup->addAction(m_editActions[ClearAll]);
}
if (!popup->isEmpty()) {
popup->addSeparator();
}
action = popup->addAction(QIcon::fromTheme(QSL("edit-select-all")), tr("Select All") + ACCEL_KEY(QKeySequence::SelectAll));
action->setEnabled(!text().isEmpty() && selectedText() != text());
connect(action, SIGNAL(triggered()), SLOT(selectAll()));
popup->addSeparator();
popup->addAction(m_editActions[SelectAll]);
#if !defined(QT_NO_IM) && QT_VERSION < 0x050000
QInputContext* qic = inputContext();
@ -247,6 +230,24 @@ QMenu* LineEdit::createContextMenu(QAction* pasteAndGoAction)
return popup;
}
void LineEdit::updateActions()
{
m_editActions[Undo]->setEnabled(!isReadOnly() && isUndoAvailable());
m_editActions[Redo]->setEnabled(!isReadOnly() && isRedoAvailable());
m_editActions[Cut]->setEnabled(!isReadOnly() && hasSelectedText() && echoMode() == QLineEdit::Normal);
m_editActions[Copy]->setEnabled(hasSelectedText() && echoMode() == QLineEdit::Normal);
m_editActions[Paste]->setEnabled(!isReadOnly() && !QApplication::clipboard()->text().isEmpty());
m_editActions[Delete]->setEnabled(!isReadOnly() && hasSelectedText());
m_editActions[SelectAll]->setEnabled(!text().isEmpty() && selectedText() != text());
}
void LineEdit::slotDelete()
{
if (hasSelectedText()) {
del();
}
}
void LineEdit::addWidget(QWidget* widget, WidgetPosition position)
{
if (!widget) {
@ -350,13 +351,6 @@ void LineEdit::updateTextMargins()
setTextMargins(left, top, right, bottom);
}
void LineEdit::slotDelete()
{
if (hasSelectedText()) {
del();
}
}
void LineEdit::focusInEvent(QFocusEvent* event)
{
if (event->reason() == Qt::MouseFocusReason && qzSettings->selectAllOnClick) {

View File

@ -86,9 +86,10 @@ public:
Cut = 2,
Copy = 3,
Paste = 4,
Delete = 5,
ClearAll = 6,
SelectAll = 7
PasteAndGo = 5,
Delete = 6,
ClearAll = 7,
SelectAll = 8
};
LineEdit(QWidget* parent = 0);
@ -112,8 +113,6 @@ public slots:
void setLeftMargin(int margin);
void updateTextMargins();
void slotDelete();
protected:
void focusInEvent(QFocusEvent* event);
void mousePressEvent(QMouseEvent* event);
@ -121,7 +120,11 @@ protected:
void mouseDoubleClickEvent(QMouseEvent* event);
bool event(QEvent* event);
QMenu* createContextMenu(QAction* pasteAndGoAction);
QMenu* createContextMenu();
private slots:
void updateActions();
void slotDelete();
private:
void init();
@ -131,7 +134,7 @@ private:
QHBoxLayout* m_leftLayout;
QHBoxLayout* m_rightLayout;
QHBoxLayout* mainLayout;
QAction* m_editActions[8];
QAction* m_editActions[9];
int m_minHeight;
int m_leftMargin;

View File

@ -47,8 +47,6 @@ LocationBar::LocationBar(BrowserWindow* window)
: LineEdit(window)
, m_window(window)
, m_webView(0)
, m_pasteAndGoAction(0)
, m_clearAction(0)
, m_holdingAlt(false)
, m_backspacePressed(false)
, m_loadProgress(0)
@ -88,6 +86,10 @@ LocationBar::LocationBar(BrowserWindow* window)
domainCompleter->setModel(m_domainCompleterModel);
setCompleter(domainCompleter);
editAction(PasteAndGo)->setText(tr("Paste And &Go"));
editAction(PasteAndGo)->setIcon(QIcon::fromTheme(QSL("edit-paste")));
connect(editAction(PasteAndGo), SIGNAL(triggered()), this, SLOT(pasteAndGo()));
connect(this, SIGNAL(textEdited(QString)), this, SLOT(textEditted()));
connect(m_goIcon, SIGNAL(clicked(QPoint)), this, SLOT(requestLoadUrl()));
connect(down, SIGNAL(clicked(QPoint)), m_completer, SLOT(showMostVisited()));
@ -361,13 +363,7 @@ void LocationBar::pasteAndGo()
void LocationBar::contextMenuEvent(QContextMenuEvent* event)
{
if (!m_pasteAndGoAction) {
m_pasteAndGoAction = new QAction(QIcon::fromTheme("edit-paste"), tr("Paste And &Go"), this);
m_pasteAndGoAction->setShortcut(QKeySequence("Ctrl+Shift+V"));
connect(m_pasteAndGoAction, SIGNAL(triggered()), this, SLOT(pasteAndGo()));
}
QMenu* menu = createContextMenu(m_pasteAndGoAction);
QMenu* menu = createContextMenu();
menu->setAttribute(Qt::WA_DeleteOnClose);
// Prevent choosing first option with double rightclick

View File

@ -103,9 +103,6 @@ private:
BrowserWindow* m_window;
TabbedWebView* m_webView;
QAction* m_pasteAndGoAction;
QAction* m_clearAction;
bool m_rssIconVisible;
bool m_holdingAlt;
bool m_backspacePressed;

View File

@ -56,8 +56,6 @@ void WebSearchBar_Button::contextMenuEvent(QContextMenuEvent* event)
WebSearchBar::WebSearchBar(BrowserWindow* window)
: LineEdit(window)
, m_window(window)
, m_pasteAndGoAction(0)
, m_clearAction(0)
, m_reloadingEngines(false)
{
setObjectName("websearchbar");
@ -97,6 +95,10 @@ WebSearchBar::WebSearchBar(BrowserWindow* window)
connect(m_openSearchEngine, SIGNAL(suggestions(QStringList)), this, SLOT(addSuggestions(QStringList)));
connect(this, SIGNAL(textEdited(QString)), m_openSearchEngine, SLOT(requestSuggestions(QString)));
editAction(PasteAndGo)->setText(tr("Paste And &Search"));
editAction(PasteAndGo)->setIcon(QIcon::fromTheme(QSL("edit-paste")));
connect(editAction(PasteAndGo), SIGNAL(triggered()), this, SLOT(pasteAndGo()));
QTimer::singleShot(0, this, SLOT(setupEngines()));
}
@ -257,13 +259,7 @@ void WebSearchBar::contextMenuEvent(QContextMenuEvent* event)
{
Q_UNUSED(event)
if (!m_pasteAndGoAction) {
m_pasteAndGoAction = new QAction(QIcon::fromTheme("edit-paste"), tr("Paste And &Search"), this);
m_pasteAndGoAction->setShortcut(QKeySequence("Ctrl+Shift+V"));
connect(m_pasteAndGoAction, SIGNAL(triggered()), this, SLOT(pasteAndGo()));
}
QMenu* menu = createContextMenu(m_pasteAndGoAction);
QMenu* menu = createContextMenu();
menu->setAttribute(Qt::WA_DeleteOnClose);
menu->addSeparator();

View File

@ -89,9 +89,6 @@ private:
SearchEnginesManager* m_searchManager;
QPointer<SearchEnginesDialog> m_searchDialog;
QAction* m_pasteAndGoAction;
QAction* m_clearAction;
bool m_reloadingEngines;
};