History find and protect; Use history_result_approximate and history_result, instead of separate history_exact, in config file

This commit is contained in:
Hanna K 2021-09-04 15:16:27 +02:00
parent ebd1dc42f3
commit aefcfdc181
4 changed files with 104 additions and 16 deletions

View File

@ -19,6 +19,12 @@
#include <QClipboard>
#include <QTextDocumentFragment>
#include <QScrollBar>
#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QDialogButtonBox>
#include <QPushButton>
#include <QDialog>
#include <QDebug>
#include <libqalculate/qalculate.h>
@ -201,6 +207,7 @@ void HistoryView::addResult(std::vector<std::string> values, std::string express
str += "</div>";
if(!initial_load) {
settings->v_expression.push_back(expression);
settings->v_protected.push_back(false);
settings->v_delexpression.push_back(false);
settings->v_result.push_back(values);
settings->v_exact.push_back(std::vector<int>());
@ -398,10 +405,22 @@ void HistoryView::inputMethodEvent(QInputMethodEvent *e) {
}
}
void HistoryView::editClear() {
clear();
s_text.clear();
settings->v_expression.clear();
settings->v_result.clear();
for(size_t i1 = 0; i1 < settings->v_protected.size(); i1++) {
if(!settings->v_protected[i1]) {
settings->v_delexpression[i1] = true;
int index = s_text.indexOf("<a href=\"" + QString::number(i1) + "\"");
if(index >= 0) {
int index2 = s_text.indexOf("<hr/>", index);
if(index2 >= 0) index2 += (5);
int index1 = s_text.lastIndexOf("<div", index);
QString new_text;
if(index1 > 0) new_text = s_text.left(index1);
if(index2 >= 0) new_text += s_text.mid(index2);
s_text = new_text;
}
}
}
setHtml("<body color=\"" + textColor().name() + "\">" + s_text + "</body>");
}
void HistoryView::editRemove() {
int i1 = -1, i2 = -1;
@ -429,6 +448,12 @@ void HistoryView::editRemove() {
setHtml("<body color=\"" + textColor().name() + "\">" + s_text + "</body>");
verticalScrollBar()->setValue(vpos);
}
void HistoryView::editProtect() {
int i1 = -1, i2 = -1;
indexAtPos(context_pos, &i1, &i2);
if(i1 < 0 || i1 >= (int) settings->v_protected.size()) return;
settings->v_protected[i1] = protectAction->isChecked();
}
void HistoryView::indexAtPos(const QPoint &pos, int *expression_index, int *result_index, QString *anchorstr) {
*expression_index = -1;
*result_index = -1;
@ -485,6 +510,13 @@ void HistoryView::contextMenuEvent(QContextMenuEvent *e) {
selectAllAction = cmenu->addAction(tr("Select All"), this, SLOT(selectAll()));
selectAllAction->setShortcut(QKeySequence::SelectAll);
selectAllAction->setShortcutContext(Qt::WidgetShortcut);
findAction = cmenu->addAction(tr("Find…"), this, SLOT(editFind()));
findAction->setShortcut(QKeySequence::Find);
findAction->setShortcutContext(Qt::WidgetShortcut);
cmenu->addSeparator();
protectAction = cmenu->addAction(tr("Protect"), this, SLOT(editProtect()));
protectAction->setCheckable(true);
cmenu->addSeparator();
delAction = cmenu->addAction(tr("Remove"), this, SLOT(editRemove()));
clearAction = cmenu->addAction(tr("Clear"), this, SLOT(editClear()));
}
@ -495,9 +527,39 @@ void HistoryView::contextMenuEvent(QContextMenuEvent *e) {
copyFormattedAction->setEnabled(textCursor().hasSelection() || (i1 >= 0 && e->reason() == QContextMenuEvent::Mouse));
selectAllAction->setEnabled(!s_text.isEmpty());
delAction->setEnabled(i1 >= 0 && e->reason() == QContextMenuEvent::Mouse && !textCursor().hasSelection());
protectAction->setChecked(i1 >= 0 && i1 < (int) settings->v_protected.size() && settings->v_protected[i1]);
protectAction->setEnabled(i1 >= 0 && e->reason() == QContextMenuEvent::Mouse && !textCursor().hasSelection());
clearAction->setEnabled(!s_text.isEmpty());
cmenu->popup(e->globalPos());
}
void HistoryView::doFind() {
if(!find(searchEdit->text())) {
QTextCursor c = textCursor();
QTextCursor cbak = c;
c.movePosition(QTextCursor::Start);
setTextCursor(c);
if(!find(searchEdit->text())) {
setTextCursor(cbak);
}
}
}
void HistoryView::editFind() {
QDialog *dialog = new QDialog(this);
QVBoxLayout *box = new QVBoxLayout(dialog);
if(settings->always_on_top) dialog->setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
dialog->setWindowTitle(tr("Find"));
QGridLayout *grid = new QGridLayout();
grid->addWidget(new QLabel(tr("Text:"), this), 0, 0);
searchEdit = new QLineEdit(this);
grid->addWidget(searchEdit, 0, 1);
box->addLayout(grid);
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Close, Qt::Horizontal, dialog);
connect(buttonBox->addButton(tr("Search"), QDialogButtonBox::AcceptRole), SIGNAL(clicked()), this, SLOT(doFind()));
connect(buttonBox->button(QDialogButtonBox::Close), SIGNAL(clicked()), dialog, SLOT(reject()));
box->addWidget(buttonBox);
dialog->exec();
dialog->deleteLater();
}
void HistoryView::editCopyFormatted() {
if(textCursor().hasSelection()) {
copy();

View File

@ -19,6 +19,7 @@ class QMenu;
class QAction;
class ExpressionEdit;
class QColor;
class QLineEdit;
class HistoryView : public QTextBrowser {
@ -42,9 +43,10 @@ class HistoryView : public QTextBrowser {
int i_pos;
QImage *paste_image;
QMenu *cmenu;
QAction *copyAction, *copyFormattedAction, *selectAllAction, *delAction, *clearAction;
QAction *copyAction, *copyFormattedAction, *selectAllAction, *delAction, *clearAction, *findAction, *protectAction;
QColor prev_color;
QPoint context_pos;
QLineEdit *searchEdit;
void mouseReleaseEvent(QMouseEvent *e) override;
void contextMenuEvent(QContextMenuEvent *e) override;
@ -52,12 +54,18 @@ class HistoryView : public QTextBrowser {
void inputMethodEvent(QInputMethodEvent*) override;
void changeEvent(QEvent*) override;
protected slots:
void doFind();
public slots:
void editCopy();
void editCopyFormatted();
void editClear();
void editRemove();
void editProtect();
void editFind();
signals:

View File

@ -293,18 +293,19 @@ void QalculateQtSettings::loadPreferences() {
svalue = stmp.substr(i + 1);
remove_blank_ends(svalue);
v = s2i(svalue);
if(svar == "history_expression") {
if(svar == "history_expression" || svar == "history_expression*") {
v_expression.push_back(svalue);
v_delexpression.push_back(false);
v_protected.push_back(svar[svar.length() - 1] == '*');
v_result.push_back(std::vector<std::string>());
v_exact.push_back(std::vector<int>());
v_delresult.push_back(std::vector<bool>());
} else if(svar == "history_result") {
} else if(svar == "history_result" || svar == "history_result_approximate") {
if(!v_result.empty()) {
v_result[settings->v_result.size() - 1].push_back(svalue);
v_delresult[settings->v_result.size() - 1].push_back(false);
if(v_exact[settings->v_exact.size() - 1].size() < v_result[settings->v_result.size() - 1].size()) {
v_exact[settings->v_exact.size() - 1].push_back(false);
v_exact[settings->v_exact.size() - 1].push_back(svar.length() > 20);
}
}
} else if(svar == "history_exact") {
@ -941,30 +942,46 @@ void QalculateQtSettings::savePreferences(bool) {
if(n >= 300 || i == 0) break;
i--;
}
for(; i < v_expression.size(); i++) {
if(!v_delexpression[i]) {
fprintf(file, "history_expression=%s\n", v_expression[i].c_str());
size_t i_first = i;
for(i = 0; i < i_first; i++) {
if(!v_delexpression[i] && v_protected[i]) {
fprintf(file, "history_expression*=%s\n", v_expression[i].c_str());
n++;
for(size_t i2 = 0; i2 < settings->v_result[i].size(); i2++) {
if(!v_delresult[i][i2]) {
fprintf(file, "history_exact=%i\n", v_exact[i][i2]);
if(v_result[i][i2].length() > 6000) {
if(v_exact[i][i2]) fprintf(file, "history_result");
else fprintf(file, "history_result_approximate");
if(v_result[i][i2].length() > 6000 && !v_protected[i]) {
std::string str = unhtmlize(v_result[i][i2]);
if(str.length() > 5000) {
int index = 50;
while(index >= 0 && str[index] < 0 && (unsigned char) str[index + 1] < 0xC0) index--;
gsub("\n", "<br>", str);
fprintf(file, "history_result=%s …\n", str.substr(0, index + 1).c_str());
fprintf(file, "=%s …\n", str.substr(0, index + 1).c_str());
} else {
fprintf(file, "history_result=%s\n", v_result[i][i2].c_str());
fprintf(file, "=%s\n", v_result[i][i2].c_str());
}
} else {
fprintf(file, "history_result=%s\n", v_result[i][i2].c_str());
fprintf(file, "=%s\n", v_result[i][i2].c_str());
}
}
}
}
}
for(i = i_first; i < v_expression.size(); i++) {
if(!v_delexpression[i]) {
if(v_protected[i]) fprintf(file, "history_expression*=%s\n", v_expression[i].c_str());
else fprintf(file, "history_expression=%s\n", v_expression[i].c_str());
n++;
for(size_t i2 = 0; i2 < settings->v_result[i].size(); i2++) {
if(!v_delresult[i][i2]) {
if(v_exact[i][i2]) fprintf(file, "history_result");
else fprintf(file, "history_result_approximate");
fprintf(file, "=%s\n", v_result[i][i2].c_str());
}
}
}
}
}
for(size_t i = 0; i < expression_history.size(); i++) {
gsub("\n", " ", expression_history[i]);

View File

@ -118,6 +118,7 @@ class QalculateQtSettings : QObject {
std::string last_found_version;
std::vector<std::string> v_expression;
std::vector<bool> v_protected;
std::vector<bool> v_delexpression;
std::vector<std::vector<std::string> > v_result;
std::vector<std::vector<bool> > v_delresult;