Commit b3169701 authored by Steffen Imhof's avatar Steffen Imhof Committed by Thorbjørn Lindeijer

Add uppercase and lowercase selection actions to the text editor.

Merge-request: 256
Reviewed-by: default avatarThorbjørn Lindeijer <thorbjorn.lindeijer@nokia.com>
parent ed8dd0b0
......@@ -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);
}
......@@ -62,6 +62,7 @@ namespace Internal {
class BaseTextEditorPrivate;
class TextEditorOverlay;
typedef QList<RefactorMarker> 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
......
......@@ -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)
......
......@@ -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<BaseTextEditorWidget> m_currentEditor;
......
......@@ -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";
......
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