SearchViewList : Code refactoring + When you select a text and press Backspace/Delete it removes the text and if you press a character key it'll replace that selected text with the key
This commit is contained in:
parent
9f6d7101fe
commit
c2057f7f9e
|
@ -2,7 +2,6 @@
|
|||
#include "ui_SearchListView.h"
|
||||
#include "FlickerThread.h"
|
||||
|
||||
#include <QMessageBox>
|
||||
|
||||
SearchListView::SearchListView(QWidget* parent) :
|
||||
QWidget(parent),
|
||||
|
@ -187,6 +186,81 @@ void SearchListView::on_checkBoxRegex_toggled(bool checked)
|
|||
searchTextChanged(ui->searchBox->text());
|
||||
}
|
||||
|
||||
void SearchListView::addCharToSearchBox(char ch)
|
||||
{
|
||||
QString newText;
|
||||
newText = mSearchBox->text();
|
||||
|
||||
// If text is selected
|
||||
if(mSearchBox->hasSelectedText())
|
||||
{
|
||||
QString selectedText = mSearchBox->selectedText();
|
||||
int indexOfSelectedText = newText.indexOf(selectedText);
|
||||
|
||||
if(indexOfSelectedText != -1)
|
||||
{
|
||||
newText.replace(indexOfSelectedText, selectedText.length(), QString(ch));
|
||||
mCursorPosition = indexOfSelectedText;
|
||||
mSearchBox->setText(newText);
|
||||
}
|
||||
|
||||
}
|
||||
// No text selected
|
||||
else
|
||||
{
|
||||
mSearchBox->setText(mSearchBox->text().insert(mSearchBox->cursorPosition(), QString(ch)));
|
||||
mCursorPosition++;
|
||||
}
|
||||
}
|
||||
|
||||
void SearchListView::deleteTextFromSearchBox(QKeyEvent *keyEvent)
|
||||
{
|
||||
QString newText;
|
||||
newText = mSearchBox->text();
|
||||
|
||||
// If Ctrl key pressed
|
||||
if(keyEvent->modifiers() == Qt::ControlModifier)
|
||||
{
|
||||
newText = "";
|
||||
mCursorPosition = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// If text is selected
|
||||
if(mSearchBox->hasSelectedText())
|
||||
{
|
||||
QString selectedText = mSearchBox->selectedText();
|
||||
int indexOfSelectedText = newText.indexOf(selectedText);
|
||||
|
||||
if(indexOfSelectedText != -1)
|
||||
{
|
||||
newText.remove(indexOfSelectedText, selectedText.length());
|
||||
mCursorPosition = indexOfSelectedText;
|
||||
}
|
||||
}
|
||||
// No text selected
|
||||
else
|
||||
{
|
||||
// Backspace Key
|
||||
if(keyEvent->key() == Qt::Key_Backspace)
|
||||
{
|
||||
if(mCursorPosition > 0)
|
||||
newText.remove(mCursorPosition-1, 1);
|
||||
|
||||
(mCursorPosition - 1) < 0 ? mCursorPosition = 0 : mCursorPosition--;
|
||||
}
|
||||
// Delete Key
|
||||
else
|
||||
{
|
||||
if(mCursorPosition < newText.length())
|
||||
newText.remove(mCursorPosition, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mSearchBox->setText(newText);
|
||||
}
|
||||
|
||||
bool SearchListView::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
// Click in searchBox
|
||||
|
@ -209,40 +283,25 @@ bool SearchListView::eventFilter(QObject *obj, QEvent *event)
|
|||
QKeyEvent *keyEvent = static_cast<QKeyEvent* >(event);
|
||||
char ch = keyEvent->text().toUtf8().constData()[0];
|
||||
|
||||
if(isprint(ch)) //add a char to the search box
|
||||
{
|
||||
mSearchBox->setText(mSearchBox->text().insert(mSearchBox->cursorPosition(), QString(QChar(ch))));
|
||||
mCursorPosition++;
|
||||
}
|
||||
else if(keyEvent->key() == Qt::Key_Backspace) //remove a char from the search box
|
||||
{
|
||||
QString newText;
|
||||
if(keyEvent->modifiers() == Qt::ControlModifier) //clear the search box
|
||||
{
|
||||
newText = "";
|
||||
mCursorPosition = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
newText = mSearchBox->text();
|
||||
//add a char to the search box
|
||||
if(isprint(ch))
|
||||
addCharToSearchBox(ch);
|
||||
|
||||
if(mCursorPosition > 0)
|
||||
newText.remove(mCursorPosition-1, 1);
|
||||
//remove a char from the search box
|
||||
else if(keyEvent->key() == Qt::Key_Backspace || keyEvent->key() == Qt::Key_Delete)
|
||||
deleteTextFromSearchBox(keyEvent);
|
||||
|
||||
((mCursorPosition - 1) < 0) ? mCursorPosition = 0 : mCursorPosition--;
|
||||
}
|
||||
mSearchBox->setText(newText);
|
||||
}
|
||||
else if((keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)) //user pressed enter
|
||||
{
|
||||
// user pressed enter
|
||||
else if((keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter))
|
||||
if(mCurList->getCellContent(mCurList->getInitialSelection(), 0).length())
|
||||
emit enterPressedSignal();
|
||||
}
|
||||
else if(keyEvent->key() == Qt::Key_Escape) // Press escape, clears the search box
|
||||
{
|
||||
mSearchBox->clear();
|
||||
mCursorPosition = 0;
|
||||
}
|
||||
|
||||
// Press escape, clears the search box
|
||||
else if(keyEvent->key() == Qt::Key_Escape)
|
||||
{
|
||||
mSearchBox->clear();
|
||||
mCursorPosition = 0;
|
||||
}
|
||||
|
||||
// Update cursorPosition to avoid a weird bug
|
||||
mSearchBox->setCursorPosition(mCursorPosition);
|
||||
|
|
|
@ -47,6 +47,8 @@ private:
|
|||
QWidget* mListPlaceHolder;
|
||||
QAction* mSearchAction;
|
||||
int mCursorPosition;
|
||||
void addCharToSearchBox(char ch);
|
||||
void deleteTextFromSearchBox(QKeyEvent *keyEvent);
|
||||
|
||||
protected:
|
||||
bool eventFilter(QObject *obj, QEvent *event);
|
||||
|
|
Loading…
Reference in New Issue