Open action editor: ensure that data is updated whenever the current row changes, not only on a mouse click. General cleanup

This commit is contained in:
Jean-Francois Dockes 2019-08-27 07:50:27 +02:00
parent 3a668a6b22
commit e0d300ff58
2 changed files with 109 additions and 133 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2006 J.F.Dockes /* Copyright (C) 2006-2019 J.F.Dockes
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -16,49 +16,41 @@
*/ */
#include "autoconfig.h" #include "autoconfig.h"
#include <stdio.h> #include "viewaction_w.h"
#include <vector> #include <vector>
#include <utility> #include <utility>
#include <string> #include <string>
using namespace std; #include <QMessageBox>
#include <qpushbutton.h>
#include <qtimer.h>
#include <qlistwidget.h>
#include <qmessagebox.h>
#include <qinputdialog.h>
#include <qlayout.h>
#include "recoll.h" #include "recoll.h"
#include "log.h" #include "log.h"
#include "guiutils.h" #include "guiutils.h"
#include "viewaction_w.h" using namespace std;
void ViewAction::init() void ViewAction::init()
{ {
selSamePB->setEnabled(false); selSamePB->setEnabled(false);
connect(closePB, SIGNAL(clicked()), this, SLOT(close())); connect(closePB, SIGNAL(clicked()), this, SLOT(close()));
connect(chgActPB, SIGNAL(clicked()), this, SLOT(editActions())); connect(chgActPB, SIGNAL(clicked()), this, SLOT(editActions()));
connect(actionsLV,SIGNAL(itemDoubleClicked(QTableWidgetItem *)), connect(actionsLV,
this, SLOT(onItemDoubleClicked(QTableWidgetItem *))); SIGNAL(currentItemChanged(QTableWidgetItem *, QTableWidgetItem *)),
connect(actionsLV,SIGNAL(itemClicked(QTableWidgetItem *)), this,
this, SLOT(onItemClicked(QTableWidgetItem *))); SLOT(onCurrentItemChanged(QTableWidgetItem *, QTableWidgetItem *)));
useDesktopCB->setChecked(prefs.useDesktopOpen); useDesktopCB->setChecked(prefs.useDesktopOpen);
onUseDesktopCBToggled(prefs.useDesktopOpen); onUseDesktopCBToggled(prefs.useDesktopOpen);
connect(useDesktopCB, SIGNAL(stateChanged(int)), connect(useDesktopCB, SIGNAL(stateChanged(int)),
this, SLOT(onUseDesktopCBToggled(int))); this, SLOT(onUseDesktopCBToggled(int)));
connect(setExceptCB, SIGNAL(stateChanged(int)), connect(setExceptCB, SIGNAL(stateChanged(int)),
this, SLOT(onSetExceptCBToggled(int))); this, SLOT(onSetExceptCBToggled(int)));
connect(selSamePB, SIGNAL(clicked()), connect(selSamePB, SIGNAL(clicked()),
this, SLOT(onSelSameClicked())); this, SLOT(onSelSameClicked()));
resize(QSize(640, 480).expandedTo(minimumSizeHint())); resize(QSize(640, 480).expandedTo(minimumSizeHint()));
} }
void ViewAction::onUseDesktopCBToggled(int onoff) void ViewAction::onUseDesktopCBToggled(int onoff)
{ {
prefs.useDesktopOpen = onoff != 0; prefs.useDesktopOpen = onoff != 0;
@ -79,26 +71,23 @@ void ViewAction::fillLists()
vector<pair<string, string> > defs; vector<pair<string, string> > defs;
theconfig->getMimeViewerDefs(defs); theconfig->getMimeViewerDefs(defs);
actionsLV->setRowCount(defs.size()); actionsLV->setRowCount(defs.size());
int row = 0;
set<string> viewerXs; set<string> viewerXs;
if (prefs.useDesktopOpen) { if (prefs.useDesktopOpen) {
viewerXs = theconfig->getMimeViewerAllEx(); viewerXs = theconfig->getMimeViewerAllEx();
} }
for (vector<pair<string, string> >::const_iterator it = defs.begin();
it != defs.end(); it++) { int row = 0;
actionsLV->setItem(row, 0, for (const auto& def : defs) {
new QTableWidgetItem(QString::fromUtf8(it->first.c_str()))); actionsLV->setItem(row, 0, new QTableWidgetItem(u8s2qs(def.first)));
if (!prefs.useDesktopOpen || if (!prefs.useDesktopOpen ||
viewerXs.find(it->first) != viewerXs.end()) { viewerXs.find(def.first) != viewerXs.end()) {
actionsLV->setItem( actionsLV->setItem(row, 1, new QTableWidgetItem(u8s2qs(def.second)));
row, 1, } else {
new QTableWidgetItem(QString::fromUtf8(it->second.c_str()))); actionsLV->setItem(
} else { row, 1, new QTableWidgetItem(tr("Desktop Default")));
actionsLV->setItem( }
row, 1, new QTableWidgetItem(tr("Desktop Default"))); row++;
}
row++;
} }
QStringList labels(tr("MIME type")); QStringList labels(tr("MIME type"));
labels.push_back(tr("Command")); labels.push_back(tr("Command"));
@ -109,11 +98,11 @@ void ViewAction::selectMT(const QString& mt)
{ {
actionsLV->clearSelection(); actionsLV->clearSelection();
QList<QTableWidgetItem *>items = QList<QTableWidgetItem *>items =
actionsLV->findItems(mt, Qt::MatchFixedString|Qt::MatchCaseSensitive); actionsLV->findItems(mt, Qt::MatchFixedString|Qt::MatchCaseSensitive);
for (QList<QTableWidgetItem *>::iterator it = items.begin(); for (QList<QTableWidgetItem *>::iterator it = items.begin();
it != items.end(); it++) { it != items.end(); it++) {
(*it)->setSelected(true); (*it)->setSelected(true);
actionsLV->setCurrentItem(*it, QItemSelectionModel::Columns); actionsLV->setCurrentItem(*it, QItemSelectionModel::Columns);
} }
} }
@ -122,52 +111,44 @@ void ViewAction::onSelSameClicked()
actionsLV->clearSelection(); actionsLV->clearSelection();
QString value = currentLBL->text(); QString value = currentLBL->text();
if (value.isEmpty()) if (value.isEmpty())
return; return;
string action = qs2utf8s(value); string action = qs2utf8s(value);
fprintf(stderr, "value: %s\n", action.c_str()); LOGDEB1("ViewAction::onSelSameClicked: value: " << action << endl);
vector<pair<string, string> > defs; vector<pair<string, string> > defs;
theconfig->getMimeViewerDefs(defs); theconfig->getMimeViewerDefs(defs);
for (unsigned int i = 0; i < defs.size(); i++) { for (const auto& def : defs) {
if (defs[i].second == action) { if (def.second == action) {
QList<QTableWidgetItem *>items = QList<QTableWidgetItem *>items = actionsLV->findItems(
actionsLV->findItems(QString::fromUtf8(defs[i].first.c_str()), u8s2qs(def.first), Qt::MatchFixedString|Qt::MatchCaseSensitive);
Qt::MatchFixedString|Qt::MatchCaseSensitive); for (QList<QTableWidgetItem *>::iterator it = items.begin();
for (QList<QTableWidgetItem *>::iterator it = items.begin(); it != items.end(); it++) {
it != items.end(); it++) { (*it)->setSelected(true);
(*it)->setSelected(true); actionsLV->item((*it)->row(), 1)->setSelected(true);
QTableWidgetItem *item1 = actionsLV->item((*it)->row(), 1); }
item1->setSelected(true); }
}
}
} }
} }
// Fill the input fields with the row's values when the user clicks void ViewAction::onCurrentItemChanged(QTableWidgetItem *item, QTableWidgetItem *)
void ViewAction::onItemClicked(QTableWidgetItem * item)
{ {
QTableWidgetItem *item0 = actionsLV->item(item->row(), 0);
string mtype = (const char *)item0->text().toLocal8Bit();
vector<pair<string, string> > defs;
theconfig->getMimeViewerDefs(defs);
for (unsigned int i = 0; i < defs.size(); i++) {
if (defs[i].first == mtype) {
currentLBL->setText(QString::fromUtf8(defs[i].second.c_str()));
selSamePB->setEnabled(true);
return;
}
}
currentLBL->clear(); currentLBL->clear();
selSamePB->setEnabled(false); selSamePB->setEnabled(false);
} if (nullptr == item) {
return;
void ViewAction::onItemDoubleClicked(QTableWidgetItem * item) }
{
actionsLV->clearSelection();
item->setSelected(true);
QTableWidgetItem *item0 = actionsLV->item(item->row(), 0); QTableWidgetItem *item0 = actionsLV->item(item->row(), 0);
item0->setSelected(true); string mtype = qs2utf8s(item0->text());
editActions();
vector<pair<string, string> > defs;
theconfig->getMimeViewerDefs(defs);
for (const auto& def : defs) {
if (def.first == mtype) {
currentLBL->setText(u8s2qs(def.second));
selSamePB->setEnabled(true);
return;
}
}
} }
void ViewAction::editActions() void ViewAction::editActions()
@ -176,58 +157,58 @@ void ViewAction::editActions()
int except0 = -1; int except0 = -1;
set<string> viewerXs = theconfig->getMimeViewerAllEx(); set<string> viewerXs = theconfig->getMimeViewerAllEx();
vector<string> mtypes;
list<string> mtypes;
bool dowarnmultiple = true; bool dowarnmultiple = true;
for (int row = 0; row < actionsLV->rowCount(); row++) { for (int row = 0; row < actionsLV->rowCount(); row++) {
QTableWidgetItem *item0 = actionsLV->item(row, 0); QTableWidgetItem *item0 = actionsLV->item(row, 0);
if (!item0->isSelected()) if (!item0->isSelected())
continue; continue;
string mtype = (const char *)item0->text().toLocal8Bit(); string mtype = qs2utf8s(item0->text());
mtypes.push_back(mtype); mtypes.push_back(mtype);
QTableWidgetItem *item1 = actionsLV->item(row, 1); QTableWidgetItem *item1 = actionsLV->item(row, 1);
QString action = item1->text(); QString action = item1->text();
int except = viewerXs.find(mtype) != viewerXs.end(); bool except = viewerXs.find(mtype) != viewerXs.end();
if (action0.isEmpty()) { if (action0.isEmpty()) {
action0 = action; action0 = action;
except0 = except; except0 = except;
} else { } else {
if ((action != action0 || except != except0) && dowarnmultiple) { if ((action != action0 || except != except0) && dowarnmultiple) {
switch (QMessageBox::warning(0, "Recoll", switch (QMessageBox::warning(0, "Recoll",
tr("Changing entries with " tr("Changing entries with "
"different current values"), "different current values"),
"Continue", "Continue",
"Cancel", "Cancel",
0, 0, 1)) { 0, 0, 1)) {
case 0: dowarnmultiple = false;break; case 0: dowarnmultiple = false; break;
case 1: return; case 1: return;
} }
} }
} }
} }
if (action0.isEmpty())
return;
string sact = (const char *)newActionLE->text().toLocal8Bit(); if (action0.isEmpty())
trimstring(sact); return;
string sact = qs2utf8s(newActionLE->text());
if (!sact.empty()) {
trimstring(sact);
#ifdef _WIN32 #ifdef _WIN32
path_slashize(sact); path_slashize(sact);
#endif #endif
for (list<string>::const_iterator mit = mtypes.begin(); }
mit != mtypes.end(); mit++) { for (const auto& entry : mtypes) {
set<string>::iterator xit = viewerXs.find(*mit); auto xit = viewerXs.find(entry);
if (setExceptCB->isChecked()) { if (setExceptCB->isChecked()) {
if (xit == viewerXs.end()) { if (xit == viewerXs.end()) {
viewerXs.insert(*mit); viewerXs.insert(entry);
} }
} else { } else {
if (xit != viewerXs.end()) { if (xit != viewerXs.end()) {
viewerXs.erase(xit); viewerXs.erase(xit);
} }
} }
// An empty action will restore the default (erase from // An empty action will restore the default (erase from
// topmost conftree) // topmost conftree)
theconfig->setMimeViewerDef(*mit, sact); theconfig->setMimeViewerDef(entry, sact);
} }
theconfig->setMimeViewerAllEx(viewerXs); theconfig->setMimeViewerAllEx(viewerXs);

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2006 J.F.Dockes /* Copyright (C) 2006-2019 J.F.Dockes
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
@ -17,7 +17,6 @@
#ifndef _VIEWACTION_W_H_INCLUDED_ #ifndef _VIEWACTION_W_H_INCLUDED_
#define _VIEWACTION_W_H_INCLUDED_ #define _VIEWACTION_W_H_INCLUDED_
#include <qvariant.h>
#include <qdialog.h> #include <qdialog.h>
#include "ui_viewaction.h" #include "ui_viewaction.h"
@ -32,18 +31,16 @@ class ViewAction : public QDialog, public Ui::ViewActionBase
public: public:
ViewAction(QWidget* parent = 0) ViewAction(QWidget* parent = 0)
: QDialog(parent) : QDialog(parent) {
{ setupUi(this);
setupUi(this); init();
init();
} }
~ViewAction() {} ~ViewAction() {}
void selectMT(const QString& mt); void selectMT(const QString& mt);
public slots: public slots:
virtual void editActions(); virtual void editActions();
virtual void onItemClicked(QTableWidgetItem *); virtual void onCurrentItemChanged(QTableWidgetItem *, QTableWidgetItem *);
virtual void onItemDoubleClicked(QTableWidgetItem *);
virtual void onUseDesktopCBToggled(int); virtual void onUseDesktopCBToggled(int);
virtual void onSetExceptCBToggled(int); virtual void onSetExceptCBToggled(int);
virtual void onSelSameClicked(); virtual void onSelSameClicked();
@ -52,6 +49,4 @@ private:
virtual void fillLists(); virtual void fillLists();
}; };
#endif /* _VIEWACTION_W_H_INCLUDED_ */ #endif /* _VIEWACTION_W_H_INCLUDED_ */