fix: last line not removed when it's not used in the Drag&Drop

This commit is contained in:
Roy Qu 2023-03-24 18:24:48 +08:00
parent 6be2b79e01
commit 584b970aec
5 changed files with 5 additions and 633 deletions

View File

@ -52,8 +52,6 @@
#include "visithistorymanager.h"
#include "widgets/projectalreadyopendialog.h"
#include "widgets/searchdialog.h"
#include "widgets/replacedialog.h"
#include <QCloseEvent>
#include <QComboBox>

View File

@ -1,212 +0,0 @@
#include "replacedialog.h"
#include "ui_replacedialog.h"
#include <QMessageBox>
#include <memory>
#include <qsynedit/searcher/basicsearcher.h>
#include <qsynedit/searcher/regexsearcher.h>
#include "../utils.h"
#include "../editor.h"
#include "../editorlist.h"
#include "../mainwindow.h"
ReplaceDialog::ReplaceDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::ReplaceDialog),
mSearchOptions()
{
ui->setupUi(this);
mBasicSearchEngine= std::make_shared<QSynedit::BasicSearcher>();
mRegexSearchEngine= std::make_shared<QSynedit::RegexSearcher>();
}
ReplaceDialog::~ReplaceDialog()
{
delete ui;
}
void ReplaceDialog::replace(const QString &text)
{
ui->cbFind->setCurrentText(text);
ui->cbFind->setFocus();
//ui->cbReplace->setCurrentText("");
show();
}
void ReplaceDialog::doSearch(bool backward)
{
saveComboHistory(ui->cbFind,ui->cbFind->currentText());
prepareOptions(backward);
Editor *editor = pMainWindow->editorList()->getEditor();
if (editor) {
// Modify the caret when using 'from cursor' and when the selection is ignored
if (!mSearchOptions.testFlag(QSynedit::ssoEntireScope) && !mSearchOptions.testFlag(QSynedit::ssoSelectedOnly)
&& editor->selAvail()) {
// start at end of selection
if (mSearchOptions.testFlag(QSynedit::ssoBackwards)) {
editor->setCaretXY(editor->blockBegin());
} else {
editor->setCaretXY(editor->blockEnd());
}
}
QSynedit::PSynSearchBase searchEngine;
if (mSearchOptions.testFlag(QSynedit::ssoRegExp)) {
searchEngine = mRegexSearchEngine;
} else {
searchEngine = mBasicSearchEngine;
}
editor->searchReplace(
ui->cbFind->currentText(),
"",
mSearchOptions,
searchEngine, nullptr, [this,backward](){
QString msg;
if (backward) {
msg = tr("Beginning of file has been reached. ")
+tr("Do you want to continue from file's end?");
} else {
msg = tr("End of file has been reached. ")
+tr("Do you want to continue from file's beginning?");
}
QWidget *p;
if (isVisible()) {
p=this;
} else {
p=pMainWindow;
}
return QMessageBox::question(p,
tr("Continue Search"),
msg,
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes) == QMessageBox::Yes;
});
}
}
void ReplaceDialog::doReplace(bool replaceAll)
{
saveComboHistory(ui->cbFind,ui->cbFind->currentText());
saveComboHistory(ui->cbReplace,ui->cbReplace->currentText());
prepareOptions(false);
Editor *editor = pMainWindow->editorList()->getEditor();
if (editor) {
// // Modify the caret when using 'from cursor' and when the selection is ignored
// if (!mSearchOptions.testFlag(QSynedit::ssoEntireScope) && !mSearchOptions.testFlag(QSynedit::ssoSelectedOnly)
// && editor->selAvail()) {
// // start at end of selection
// if (mSearchOptions.testFlag(QSynedit::ssoBackwards)) {
// editor->setCaretXY(editor->blockBegin());
// } else {
// editor->setCaretXY(editor->blockEnd());
// }
// }
QSynedit::PSynSearchBase searchEngine;
if (mSearchOptions.testFlag(QSynedit::ssoRegExp)) {
searchEngine = mRegexSearchEngine;
} else {
searchEngine = mBasicSearchEngine;
}
editor->searchReplace(
ui->cbFind->currentText(),
ui->cbReplace->currentText(),
mSearchOptions,
searchEngine,
[&replaceAll](const QString& /*sSearch*/,
const QString& /*sReplace*/, int /*Line*/, int /*ch*/, int /*wordLen*/){
if (replaceAll) {
return QSynedit::SearchAction::ReplaceAll;
} else {
return QSynedit::SearchAction::ReplaceAndExit;
}
},
[this](){
QString msg = tr("End of file has been reached. ")
+tr("Do you want to continue from file's beginning?");
QWidget *p;
if (isVisible()) {
p=this;
} else {
p=pMainWindow;
}
return QMessageBox::question(p,
tr("Continue Search"),
msg,
QMessageBox::Yes|QMessageBox::No,
QMessageBox::Yes) == QMessageBox::Yes;
});
}
}
void ReplaceDialog::prepareOptions(bool backward)
{
mSearchOptions&=0;
// Apply options
if (backward) {
mSearchOptions.setFlag(QSynedit::ssoBackwards);
}
if (ui->chkRegExp->isChecked()) {
mSearchOptions.setFlag(QSynedit::ssoRegExp);
}
if (ui->chkCaseSensetive->isChecked()) {
mSearchOptions.setFlag(QSynedit::ssoMatchCase);
}
if (ui->chkWholeWord->isChecked()) {
mSearchOptions.setFlag(QSynedit::ssoWholeWord);
}
if (ui->chkWrapAround->isChecked()) {
mSearchOptions.setFlag(QSynedit::ssoWrapAround);
}
// Apply scope, when enabled
if (ui->grpScope->isEnabled()) {
if (ui->rbSelection->isChecked()) {
mSearchOptions.setFlag(QSynedit::ssoSelectedOnly);
}
}
// Apply origin, when enabled
if (ui->grpOrigin->isEnabled()) {
if (ui->rbEntireScope->isChecked()) {
mSearchOptions.setFlag(QSynedit::ssoEntireScope);
}
}
}
void ReplaceDialog::on_cbFind_currentTextChanged(const QString &value)
{
ui->btnNext->setEnabled(!value.isEmpty());
ui->btnPrevious->setEnabled(!value.isEmpty());
ui->btnReplace->setEnabled(!value.isEmpty());
ui->btnReplaceAll->setEnabled(!value.isEmpty());
}
void ReplaceDialog::on_btnClose_clicked()
{
close();
}
void ReplaceDialog::on_btnNext_clicked()
{
doSearch(false);
}
void ReplaceDialog::on_btnPrevious_clicked()
{
doSearch(true);
}
void ReplaceDialog::on_btnReplace_clicked()
{
doReplace(false);
}
void ReplaceDialog::on_btnReplaceAll_clicked()
{
doReplace(true);
close();
}

View File

@ -1,42 +0,0 @@
#ifndef REPLACEDIALOG_H
#define REPLACEDIALOG_H
#include <QDialog>
#include <qsynedit/searcher/baseseacher.h>
namespace Ui {
class ReplaceDialog;
}
class ReplaceDialog : public QDialog
{
Q_OBJECT
public:
explicit ReplaceDialog(QWidget *parent = nullptr);
~ReplaceDialog();
void replace(const QString& text);
private:
void doSearch(bool backward);
void doReplace(bool replaceAll);
void prepareOptions(bool backward);
private slots:
void on_cbFind_currentTextChanged(const QString &arg1);
void on_btnClose_clicked();
void on_btnNext_clicked();
void on_btnPrevious_clicked();
void on_btnReplace_clicked();
void on_btnReplaceAll_clicked();
private:
Ui::ReplaceDialog *ui;
QSynedit::SearchOptions mSearchOptions;
QSynedit::PSynSearchBase mBasicSearchEngine;
QSynedit::PSynSearchBase mRegexSearchEngine;
};
#endif // REPLACEDIALOG_H

View File

@ -1,376 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ReplaceDialog</class>
<widget class="QDialog" name="ReplaceDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>512</width>
<height>308</height>
</rect>
</property>
<property name="windowTitle">
<string>Replace</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QWidget" name="widget_4" native="true">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="spacing">
<number>7</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QWidget" name="widget" native="true">
<layout class="QGridLayout" name="gridLayout_3">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="lblFind">
<property name="text">
<string>Text to Find:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="cbFind">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Replace with:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="cbReplace">
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="1" column="1">
<widget class="QGroupBox" name="grpScope">
<property name="title">
<string>Scope:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<property name="leftMargin">
<number>7</number>
</property>
<property name="topMargin">
<number>7</number>
</property>
<property name="rightMargin">
<number>7</number>
</property>
<property name="bottomMargin">
<number>7</number>
</property>
<item>
<widget class="QRadioButton" name="rbGlobal">
<property name="text">
<string>Global</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="rbSelection">
<property name="text">
<string>Selection</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="1">
<widget class="QGroupBox" name="grpOrigin">
<property name="title">
<string>Origin:</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<property name="leftMargin">
<number>7</number>
</property>
<property name="topMargin">
<number>7</number>
</property>
<property name="rightMargin">
<number>7</number>
</property>
<property name="bottomMargin">
<number>7</number>
</property>
<item>
<widget class="QRadioButton" name="rbFromCursor">
<property name="text">
<string>From cursor</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="rbEntireScope">
<property name="text">
<string>Entire scope</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item row="0" column="0" rowspan="2">
<widget class="QGroupBox" name="grpOptions">
<property name="title">
<string>Options:</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<property name="leftMargin">
<number>7</number>
</property>
<property name="topMargin">
<number>7</number>
</property>
<property name="rightMargin">
<number>7</number>
</property>
<property name="bottomMargin">
<number>7</number>
</property>
<item row="4" column="0">
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0">
<widget class="QCheckBox" name="chkWrapAround">
<property name="text">
<string>Wrap Around</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QCheckBox" name="chkCaseSensetive">
<property name="text">
<string>Case Sensitive</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="chkRegExp">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Regular Expression</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="txtRegExpHelp">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string notr="true">&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;&lt;a href=&quot;https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference&quot;&gt;&lt;span style=&quot; text-decoration: underline; color:#0000ff;&quot;&gt;(?)&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="openExternalLinks">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="chkWholeWord">
<property name="text">
<string>Whole words only</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_3" native="true">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="btnPrevious">
<property name="text">
<string>Find Previous</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnNext">
<property name="text">
<string>Find Next</string>
</property>
<property name="default">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnReplace">
<property name="text">
<string>Replace</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnReplaceAll">
<property name="text">
<string>Replace All</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>15</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnClose">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@ -6368,15 +6368,19 @@ void QSynEdit::dropEvent(QDropEvent *event)
// return;
// }
coord = ensureBufferCoordValid(coord);
bool lastLineUsed = coord.line == mDocument->count();
int topLine = mTopLine;
int leftChar = mLeftChar;
int line=mDocument->count()-1;
QString s=mDocument->getLine(line-1);
QStringList text=splitStrings(event->mimeData()->text());
beginEditing();
mUndoList->addChange(ChangeReason::LineBreak,
if (lastLineUsed)
mUndoList->addChange(ChangeReason::LineBreak,
BufferCoord{s.length()+1,line},
BufferCoord{s.length()+1,line}, QStringList(), SelectionMode::Normal);
else
mDocument->deleteAt(mDocument->count()-1);
addLeftTopToUndo();
addCaretToUndo();
addSelectionToUndo();