diff --git a/src/plugins/texteditor/basetexteditor.cpp b/src/plugins/texteditor/basetexteditor.cpp index e278aae392c..5638cba01a2 100644 --- a/src/plugins/texteditor/basetexteditor.cpp +++ b/src/plugins/texteditor/basetexteditor.cpp @@ -1012,6 +1012,17 @@ void BaseTextEditorWidget::moveLineDown() moveLineUpDown(false); } +void BaseTextEditorWidget::uppercaseSelection() +{ + transformSelection(&QString::toUpper); +} + + +void BaseTextEditorWidget::lowercaseSelection() +{ + transformSelection(&QString::toLower); +} + void BaseTextEditorWidget::moveLineUpDown(bool up) { QTextCursor cursor = textCursor(); @@ -6081,3 +6092,41 @@ int BaseTextEditorWidget::rowCount() const QFontMetricsF fm(font()); return viewport()->rect().height() / fm.lineSpacing(); } + +/** + Helper method to transform a selected text. If nothing is selected at the moment + the word under the cursor is used. + The type of the transformation is determined by the method pointer given. + + @param method pointer to the QString method to use for the transformation + + @see uppercaseSelection, lowercaseSelection +*/ +void BaseTextEditorWidget::transformSelection(Internal::TransformationMethod method) +{ + QTextCursor cursor = textCursor(); + + int pos = cursor.position(); + int anchor = cursor.anchor(); + + if (!cursor.hasSelection()) { + // if nothing is selected, select the word over the cursor + cursor.select(QTextCursor::WordUnderCursor); + } + + QString text = cursor.selectedText(); + QString transformedText = (text.*method)(); + + if (transformedText == text) { + // if the transformation does not do anything to the selection, do no create an undo step + return; + } + + cursor.insertText(transformedText); + + // (re)select the changed text + // Note: this assumes the transformation did not change the length, + cursor.setPosition(anchor); + cursor.setPosition(pos, QTextCursor::KeepAnchor); + setTextCursor(cursor); +} diff --git a/src/plugins/texteditor/basetexteditor.h b/src/plugins/texteditor/basetexteditor.h index 0c1721b0b74..d36ded44f01 100644 --- a/src/plugins/texteditor/basetexteditor.h +++ b/src/plugins/texteditor/basetexteditor.h @@ -62,6 +62,7 @@ namespace Internal { class BaseTextEditorPrivate; class TextEditorOverlay; typedef QList RefactorMarkers; + typedef QString (QString::*TransformationMethod)() const; } class ITextMarkable; @@ -292,6 +293,9 @@ public slots: void insertLineAbove(); void insertLineBelow(); + void uppercaseSelection(); + void lowercaseSelection(); + void cleanWhitespace(); signals: @@ -523,6 +527,7 @@ private: bool camelCaseRight(QTextCursor &cursor, QTextCursor::MoveMode mode); bool camelCaseLeft(QTextCursor &cursor, QTextCursor::MoveMode mode); + void transformSelection(Internal::TransformationMethod method); private slots: // auto completion diff --git a/src/plugins/texteditor/texteditoractionhandler.cpp b/src/plugins/texteditor/texteditoractionhandler.cpp index 54d28eac3fb..35445b16388 100644 --- a/src/plugins/texteditor/texteditoractionhandler.cpp +++ b/src/plugins/texteditor/texteditoractionhandler.cpp @@ -93,6 +93,10 @@ TextEditorActionHandler::TextEditorActionHandler(const char *context, m_copyLineUpAction(0), m_copyLineDownAction(0), m_joinLinesAction(0), + m_insertLineAboveAction(0), + m_insertLineBelowAction(0), + m_upperCaseSelectionAction(0), + m_lowerCaseSelectionAction(0), m_optionalActions(optionalActions), m_currentEditor(0), m_contextId(context), @@ -306,6 +310,16 @@ void TextEditorActionHandler::createActions() command->setDefaultKeySequence(QKeySequence(tr("Ctrl+Return"))); connect(m_insertLineBelowAction, SIGNAL(triggered()), this, SLOT(insertLineBelow())); + m_upperCaseSelectionAction = new QAction(tr("Uppercase Selection"), this); + command = am->registerAction(m_upperCaseSelectionAction, Constants::UPPERCASE_SELECTION, m_contextId, true); + command->setDefaultKeySequence(QKeySequence(tr("Alt+Shift+U"))); + connect(m_upperCaseSelectionAction, SIGNAL(triggered()), this, SLOT(uppercaseSelection())); + + m_lowerCaseSelectionAction = new QAction(tr("Lowercase Selection"), this); + command = am->registerAction(m_lowerCaseSelectionAction, Constants::LOWERCASE_SELECTION, m_contextId, true); + command->setDefaultKeySequence(QKeySequence(tr("Alt+U"))); + connect(m_lowerCaseSelectionAction, SIGNAL(triggered()), this, SLOT(lowercaseSelection())); + QAction *a = 0; a = new QAction(tr("Goto Line Start"), this); command = am->registerAction(a, Constants::GOTO_LINE_START, m_contextId, true); @@ -532,6 +546,8 @@ FUNCTION(moveLineDown) FUNCTION(copyLineUp) FUNCTION(copyLineDown) FUNCTION(joinLines) +FUNCTION(uppercaseSelection) +FUNCTION(lowercaseSelection) FUNCTION(insertLineAbove) FUNCTION(insertLineBelow) diff --git a/src/plugins/texteditor/texteditoractionhandler.h b/src/plugins/texteditor/texteditoractionhandler.h index d4fec4f8469..fb7789a17f1 100644 --- a/src/plugins/texteditor/texteditoractionhandler.h +++ b/src/plugins/texteditor/texteditoractionhandler.h @@ -120,6 +120,8 @@ private slots: void joinLines(); void insertLineAbove(); void insertLineBelow(); + void uppercaseSelection(); + void lowercaseSelection(); void updateCurrentEditor(Core::IEditor *editor); void gotoLineStart(); @@ -181,6 +183,8 @@ private: QAction *m_joinLinesAction; QAction *m_insertLineAboveAction; QAction *m_insertLineBelowAction; + QAction *m_upperCaseSelectionAction; + QAction *m_lowerCaseSelectionAction; uint m_optionalActions; QPointer m_currentEditor; diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index 4c824b9bac0..a32921ca406 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -67,6 +67,8 @@ const char * const COPY_LINE_DOWN = "TextEditor.CopyLineDown"; const char * const JOIN_LINES = "TextEditor.JoinLines"; const char * const INSERT_LINE_ABOVE = "TextEditor.InsertLineAboveCurrentLine"; const char * const INSERT_LINE_BELOW = "TextEditor.InsertLineBelowCurrentLine"; +const char * const UPPERCASE_SELECTION = "TextEditor.UppercaseSelection"; +const char * const LOWERCASE_SELECTION = "TextEditor.LowercaseSelection"; const char * const CUT_LINE = "TextEditor.CutLine"; const char * const DELETE_LINE = "TextEditor.DeleteLine"; const char * const DELETE_WORD = "TextEditor.DeleteWord";