Skip to content

Commit

Permalink
feat: improve search in document (#25)
Browse files Browse the repository at this point in the history
Fixes tasks:
QtUiDocument
QtTsDocument
RCDocument text view
RCDocument central view
RCDocument left tree
  • Loading branch information
mgiroday committed Sep 10, 2024
1 parent 05fd9fd commit 5a51860
Show file tree
Hide file tree
Showing 17 changed files with 979 additions and 144 deletions.
1 change: 0 additions & 1 deletion src/core/qttsdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#include "qttsdocument.h"
#include "logger.h"
#include "utils/log.h"

#include <QFile>
#include <QUiLoader>
Expand Down
2 changes: 2 additions & 0 deletions src/core/qttsdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class QtTsDocument : public Document
Q_PROPERTY(QString language READ language WRITE setLanguage NOTIFY languageChanged)
Q_PROPERTY(QString sourceLanguage READ sourceLanguage WRITE setSourceLanguage NOTIFY sourceLanguageChanged)
Q_PROPERTY(QList<Core::QtTsMessage *> messages READ messages NOTIFY messagesChanged)

public:
explicit QtTsDocument(QObject *parent = nullptr);

Expand All @@ -75,6 +76,7 @@ class QtTsDocument : public Document
const QString &translation, const QString &comment = QString());
Q_INVOKABLE void setMessageContext(const QString &context, const QString &comment, const QString &source,
const QString &newContext);

QString language() const;
QString sourceLanguage() const;
QList<QtTsMessage *> messages() const;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ set(PROJECT_SOURCES
gui_constants.h
guisettings.h
guisettings.cpp
highlightsearchdelegate.h
highlightsearchdelegate.cpp
historypanel.h
historypanel.cpp
imageview.h
Expand Down
53 changes: 48 additions & 5 deletions src/gui/findwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
#include "findwidget.h"
#include "core/logger.h"
#include "core/project.h"
#include "core/qttsdocument.h"
#include "core/qtuidocument.h"
#include "core/textdocument.h"
#include "guisettings.h"
#include "qttsview.h"
#include "qtuiview.h"
#include "ui_findwidget.h"

#include <QAction>
Expand Down Expand Up @@ -86,11 +90,35 @@ QString FindWidget::findString()

void FindWidget::findNext()
{
auto qtUiDocument = qobject_cast<Core::QtUiDocument *>(Core::Project::instance()->currentDocument());
if (qtUiDocument) {
Q_EMIT search(ui->findEdit->text(), QtUiView::FindForward);
return;
}

auto qtTsDocument = qobject_cast<Core::QtTsDocument *>(Core::Project::instance()->currentDocument());
if (qtTsDocument) {
Q_EMIT search(ui->findEdit->text(), QtTsView::FindForward);
return;
}

find(findFlags());
}

void FindWidget::findPrevious()
{
auto qtUiDocument = qobject_cast<Core::QtUiDocument *>(Core::Project::instance()->currentDocument());
if (qtUiDocument) {
Q_EMIT search(ui->findEdit->text(), QtUiView::FindBackward);
return;
}

auto qtTsDocument = qobject_cast<Core::QtTsDocument *>(Core::Project::instance()->currentDocument());
if (qtTsDocument) {
Q_EMIT search(ui->findEdit->text(), QtTsView::FindBackward);
return;
}

find(findFlags() | Core::TextDocument::FindBackward);
}

Expand Down Expand Up @@ -120,9 +148,12 @@ void FindWidget::open()

void FindWidget::find(int options)
{
if (ui->findEdit->text().isEmpty())
return;
auto document = Core::Project::instance()->currentDocument();

const QString searchText = ui->findEdit->text();
if (searchText.isEmpty())
return;

if (auto textDocument = qobject_cast<Core::TextDocument *>(document))
textDocument->find(findString(), options);
}
Expand All @@ -139,18 +170,30 @@ void FindWidget::replaceAll()

void FindWidget::replace(bool onlyOne)
{
const QString &before = findString();
const QString &before = ui->findEdit->text();
const QString &after = ui->replaceEdit->text();
if (before.isEmpty())
return;

auto document = Core::Project::instance()->currentDocument();
if (auto textDocument = qobject_cast<Core::TextDocument *>(document)) {
const QString &before = findString();
if (before.isEmpty())
return;
if (onlyOne)
textDocument->replaceOne(before, after, findFlags());
else
textDocument->replaceAll(before, after, findFlags());
}
}

void FindWidget::hideEvent(QHideEvent *event)
{
Q_EMIT findWidgetHiding();
QWidget::hideEvent(event);
}

void FindWidget::showReplaceFeature(bool show)
{
ui->replaceFrame->setVisible(show);
}

} // namespace Gui
9 changes: 9 additions & 0 deletions src/gui/findwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ class FindWidget : public QWidget

void open();

void showReplaceFeature(bool show = true);

signals:
void search(const QString &text, int options);
void findWidgetHiding();

protected:
void hideEvent(QHideEvent *event) override;

private:
int findFlags() const;
QString findString();
Expand Down
Loading

0 comments on commit 5a51860

Please sign in to comment.