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 "ui_SearchListView.h"
|
||||||
#include "FlickerThread.h"
|
#include "FlickerThread.h"
|
||||||
|
|
||||||
#include <QMessageBox>
|
|
||||||
|
|
||||||
SearchListView::SearchListView(QWidget* parent) :
|
SearchListView::SearchListView(QWidget* parent) :
|
||||||
QWidget(parent),
|
QWidget(parent),
|
||||||
|
@ -187,6 +186,81 @@ void SearchListView::on_checkBoxRegex_toggled(bool checked)
|
||||||
searchTextChanged(ui->searchBox->text());
|
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)
|
bool SearchListView::eventFilter(QObject *obj, QEvent *event)
|
||||||
{
|
{
|
||||||
// Click in searchBox
|
// Click in searchBox
|
||||||
|
@ -209,40 +283,25 @@ bool SearchListView::eventFilter(QObject *obj, QEvent *event)
|
||||||
QKeyEvent *keyEvent = static_cast<QKeyEvent* >(event);
|
QKeyEvent *keyEvent = static_cast<QKeyEvent* >(event);
|
||||||
char ch = keyEvent->text().toUtf8().constData()[0];
|
char ch = keyEvent->text().toUtf8().constData()[0];
|
||||||
|
|
||||||
if(isprint(ch)) //add a char to the search box
|
//add a char to the search box
|
||||||
{
|
if(isprint(ch))
|
||||||
mSearchBox->setText(mSearchBox->text().insert(mSearchBox->cursorPosition(), QString(QChar(ch))));
|
addCharToSearchBox(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();
|
|
||||||
|
|
||||||
if(mCursorPosition > 0)
|
//remove a char from the search box
|
||||||
newText.remove(mCursorPosition-1, 1);
|
else if(keyEvent->key() == Qt::Key_Backspace || keyEvent->key() == Qt::Key_Delete)
|
||||||
|
deleteTextFromSearchBox(keyEvent);
|
||||||
|
|
||||||
((mCursorPosition - 1) < 0) ? mCursorPosition = 0 : mCursorPosition--;
|
// user pressed enter
|
||||||
}
|
else if((keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter))
|
||||||
mSearchBox->setText(newText);
|
|
||||||
}
|
|
||||||
else if((keyEvent->key() == Qt::Key_Return || keyEvent->key() == Qt::Key_Enter)) //user pressed enter
|
|
||||||
{
|
|
||||||
if(mCurList->getCellContent(mCurList->getInitialSelection(), 0).length())
|
if(mCurList->getCellContent(mCurList->getInitialSelection(), 0).length())
|
||||||
emit enterPressedSignal();
|
emit enterPressedSignal();
|
||||||
}
|
|
||||||
else if(keyEvent->key() == Qt::Key_Escape) // Press escape, clears the search box
|
// Press escape, clears the search box
|
||||||
{
|
else if(keyEvent->key() == Qt::Key_Escape)
|
||||||
mSearchBox->clear();
|
{
|
||||||
mCursorPosition = 0;
|
mSearchBox->clear();
|
||||||
}
|
mCursorPosition = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Update cursorPosition to avoid a weird bug
|
// Update cursorPosition to avoid a weird bug
|
||||||
mSearchBox->setCursorPosition(mCursorPosition);
|
mSearchBox->setCursorPosition(mCursorPosition);
|
||||||
|
|
|
@ -47,6 +47,8 @@ private:
|
||||||
QWidget* mListPlaceHolder;
|
QWidget* mListPlaceHolder;
|
||||||
QAction* mSearchAction;
|
QAction* mSearchAction;
|
||||||
int mCursorPosition;
|
int mCursorPosition;
|
||||||
|
void addCharToSearchBox(char ch);
|
||||||
|
void deleteTextFromSearchBox(QKeyEvent *keyEvent);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject *obj, QEvent *event);
|
bool eventFilter(QObject *obj, QEvent *event);
|
||||||
|
|
Loading…
Reference in New Issue