#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "global.h"
#include <QDebug>
#include <QMessageBox>
#include <QFileDialog>
#include <string>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
ui->lineEdit->setPlaceholderText("please enter string");
ui->lineEdit_2->setPlaceholderText("please enter location");
ui->lineEdit_3->setPlaceholderText("find and delete");
ui->lineEdit_4->setPlaceholderText("after location replace with");
QAction *lineEditAction = new QAction;
lineEditAction->setIcon(QIcon(":/icon/x.ico"));
ui->label->setText("");
//ui->label_2->setText("show search location");
ui->lineEdit->addAction(lineEditAction,QLineEdit::TrailingPosition);
connect(lineEditAction,SIGNAL(triggered(bool)),this,SLOT(Clear()));
connect(ui->lineEdit,SIGNAL(returnPressed()),this,SLOT(inSert()));
connect(ui->lineEdit_3,SIGNAL(returnPressed()),this,SLOT(Search()));
connect(ui->lineEdit_4,SIGNAL(returnPressed()),this,SLOT(Replace()));
//Clear();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::Clear()
{
ui->lineEdit->clear();
}
void MainWindow::Search()
{
QString res;
//ui->label_2->setText("");
int searchlength = 0;
MyString temp = textEditor.strcurrent;
QByteArray ba = ui->lineEdit_3->text().toLatin1();
char* buffer = ba.data();
//qDebug()<<temp.indexof(buffer);
while(temp.indexof(buffer)){
int start = temp.indexof(buffer);
searchlength = searchlength + start;
//ui->label_2->setText(ui->label_2->text()+'\t'+QString::number(searchlength));
res = res + '\t' + QString::number(searchlength);
temp = temp.substring(start, strlen(temp.data()) - start);
}
QMessageBox::about(this, "About",res);
}
void MainWindow::Replace()
{
int deleteflag;
textEditor.previous = push(textEditor.previous, textEditor.strcurrent.data());
MyString s = textEditor.strcurrent.data();//被替换后的返回的内容
MyString d = textEditor.strcurrent.data();//被替换的内容
QByteArray ba = ui->lineEdit_3->text().toLatin1();
char* buffer = ba.data();
ba = ui->lineEdit_4->text().toLatin1();
char* p = new char[strlen(ba.data())+1];
for(int i = 0;i < strlen(ba.data());++i){
p[i] = ba.data()[i];
}
p[strlen(ba.data())] = '\0';
int index = ui->lineEdit_2->text().toInt();
textEditor.strcurrent.replace(d, s, buffer, p, false, index-1);
textEditor.strcurrent.setchar(d.data());
Clear();
Review();
textEditor.now = push(textEditor.now, textEditor.strcurrent.data());
delete(p);
}
//输入插入位置->保存原先的字符串->插入->显示->保存插入后的字符串
void MainWindow::inSert()
{
QByteArray ba = ui->lineEdit->text().toLatin1();
char* buffer = ba.data();
int index = ui->lineEdit_2->text().toInt();
textEditor.previous = push(textEditor.previous,textEditor.strcurrent.data());
textEditor.strcurrent.insert(buffer,index - 1);
Clear();
Review();
textEditor.now = push(textEditor.now,textEditor.strcurrent.data());
//qDebug()<<QString(QLatin1String(textEditor.strcurrent.data()));
delete(buffer);
}
void MainWindow::Review()
{
ui->label->setText(QString(QLatin1String(textEditor.strcurrent.data())));
}
void MainWindow::on_pushButton_clicked()
{
int deleteflag;
textEditor.previous = push(textEditor.previous, textEditor.strcurrent.data());
MyString s = textEditor.strcurrent.data();//被替换后的返回的内容
MyString d = textEditor.strcurrent.data();//被替换的内容
QByteArray ba = ui->lineEdit_3->text().toLatin1();
char* buffer = ba.data();
int replaceflag = 0;
textEditor.strcurrent.replace(d, s, buffer, (char*)"", replaceflag);
textEditor.strcurrent.setchar(d.data());
Clear();
Review();
textEditor.now = push(textEditor.now, textEditor.strcurrent.data());
}
void MainWindow::on_pushButton_2_clicked()
{
int deleteflag;
textEditor.previous = push(textEditor.previous, textEditor.strcurrent.data());
MyString s = textEditor.strcurrent.data();//被替换后的返回的内容
MyString d = textEditor.strcurrent.data();//被替换的内容
QByteArray ba = ui->lineEdit_3->text().toLatin1();
char* buffer = ba.data();
int replaceflag = 1;
textEditor.strcurrent.replace(d, s, buffer, (char*)"", replaceflag);
textEditor.strcurrent.setchar(d.data());
Clear();
Review();
textEditor.now = push(textEditor.now, textEditor.strcurrent.data());
}
void MainWindow::on_pushButton_3_clicked()
{
int deleteflag;
textEditor.previous = push(textEditor.previous, textEditor.strcurrent.data());
MyString s = textEditor.strcurrent.data();//被替换后的返回的内容
MyString d = textEditor.strcurrent.data();//被替换的内容
QByteArray ba = ui->lineEdit_3->text().toLatin1();
char* buffer = ba.data();
ba = ui->lineEdit_4->text().toLatin1();
char* p = new char[strlen(ba.data())+1];
for(int i = 0;i < strlen(ba.data());++i){
p[i] = ba.data()[i];
}
p[strlen(ba.data())] = '\0';
int index = ui->lineEdit_2->text().toInt();
textEditor.strcurrent.replace(d, s, buffer, p, true,index - 1);
textEditor.strcurrent.setchar(d.data());
Clear();
Review();
textEditor.now = push(textEditor.now, textEditor.strcurrent.data());
delete(p);
}
void MainWindow::on_pushButton_4_clicked()
{
if(path == ""){
path = QFileDialog::getSaveFileName(this, "保存", "..", "TXT(*.txt)");
}
QFile file(path);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write(textEditor.strcurrent.data());
file.close();
}
void MainWindow::on_pushButton_5_clicked()
{
path = QFileDialog::getOpenFileName(this, "打开文件", "..", "TXT(*.txt)");
QFile file(path);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
textEditor.strcurrent.setchar(file.readAll().data());
Clear();
Review();
//QByteArray allArray = file.readAll();
//QTextCodec *codec = QTextCodec::codecForName("gb18030");
//QString allStr = codec->toUnicode(allArray);
//QString allStr = QString(allArray);
file.close();
}
void MainWindow::on_pushButton_6_clicked()
{
if (textEditor.previous != NULL)
{
textEditor.now = push(textEditor.now, textEditor.strcurrent.data());
textEditor.strcurrent = textEditor.previous->data;
Clear();
Review();
textEditor.previous = pop(textEditor.previous);
}
else {
QMessageBox::about(this, "about","no back");
}
}
void MainWindow::on_pushButton_7_clicked()
{
if (textEditor.now != NULL)
{
textEditor.previous = push(textEditor.previous, textEditor.strcurrent.data());
textEditor.strcurrent = textEditor.now->data;
Clear();
Review();
textEditor.now = pop(textEditor.now);
}
else {
QMessageBox::about(this, "about","no next");
}
}
void MainWindow::on_pushButton_8_clicked()
{
QString res;
res = res + "number of capital letters:" + QString::number(textEditor.strcurrent.CharCount().bchar) + "\n";
res = res + "number of lowercase letters:"+ QString::number(textEditor.strcurrent.CharCount().schar) + "\n";
res = res + "number of space:" + QString::number(textEditor.strcurrent.CharCount().space) + "\n";
res = res + "number of Total characters:" + QString::number(textEditor.strcurrent.CharCount().length) + "\n";
qDebug()<<textEditor.strcurrent.CharCount().len
评论0