Switch to unified view

a b/src/qtgui/ptrans_w.cpp
1
/* Copyright (C) 2006 J.F.Dockes 
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 *
12
 *   You should have received a copy of the GNU General Public License
13
 *   along with this program; if not, write to the
14
 *   Free Software Foundation, Inc.,
15
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16
 */
17
18
#include <stdio.h>
19
20
#include <vector>
21
#include <utility>
22
#include <string>
23
24
using namespace std;
25
26
#include <qpushbutton.h>
27
#include <qtimer.h>
28
29
#include <qlistwidget.h>
30
31
#include <qmessagebox.h>
32
#include <qinputdialog.h>
33
#include <qlayout.h>
34
35
#include "recoll.h"
36
#include "debuglog.h"
37
#include "guiutils.h"
38
#include "conftree.h"
39
40
#include "ptrans_w.h"
41
42
void EditTrans::init(const string& dbdir)
43
{
44
    m_dbdir = path_canon(dbdir);
45
    connect(transTW, SIGNAL(itemDoubleClicked(QTableWidgetItem *)),
46
      this, SLOT(onItemDoubleClicked(QTableWidgetItem *)));
47
    connect(cancelPB, SIGNAL(clicked()), this, SLOT(close()));
48
49
    QString lab = whatIdxLA->text();
50
    lab.append(QString::fromLocal8Bit(m_dbdir.c_str()));
51
    whatIdxLA->setText(lab);
52
53
    QStringList labels(tr("Source path"));
54
    labels.push_back(tr("Local path"));
55
    transTW->setHorizontalHeaderLabels(labels);
56
57
    ConfSimple *conftrans = theconfig->getPTrans();
58
    if (!conftrans)
59
  return;
60
61
    int row = 0;
62
    vector<string> opaths = conftrans->getNames(m_dbdir);
63
    for (vector<string>::const_iterator it = opaths.begin(); 
64
   it != opaths.end(); it++) {
65
  transTW->setRowCount(row+1);
66
  transTW->setItem(row, 0, new QTableWidgetItem(
67
               QString::fromLocal8Bit(it->c_str())));
68
  string npath;
69
  conftrans->get(*it, npath, m_dbdir);
70
  transTW->setItem(row, 1, new QTableWidgetItem(
71
               QString::fromLocal8Bit(npath.c_str())));
72
  row++;
73
    }
74
75
    resize(QSize(640, 300).expandedTo(minimumSizeHint()));
76
}
77
78
void EditTrans::onItemDoubleClicked(QTableWidgetItem *item)
79
{
80
    transTW->editItem(item);
81
}
82
83
void EditTrans::on_savePB_clicked()
84
{
85
    ConfSimple *conftrans = theconfig->getPTrans();
86
    if (!conftrans) {
87
  QMessageBox::warning(0, "Recoll", tr("Config error"));
88
  return;
89
    }
90
    conftrans->holdWrites(true);
91
    conftrans->eraseKey(m_dbdir);
92
93
    for (int row = 0; row < transTW->rowCount(); row++) {
94
  QTableWidgetItem *item0 = transTW->item(row, 0);
95
  string from = path_canon((const char *)item0->text().toLocal8Bit());
96
  QTableWidgetItem *item1 = transTW->item(row, 1);
97
  string to = path_canon((const char*)item1->text().toLocal8Bit());
98
  conftrans->set(from, to, m_dbdir);
99
    }
100
    conftrans->holdWrites(false);
101
    close();
102
}
103
104
void EditTrans::on_addPB_clicked()
105
{
106
    transTW->setRowCount(transTW->rowCount()+1);
107
    int row = transTW->rowCount()-1;
108
    transTW->setItem(row, 0, new QTableWidgetItem(tr("Original path")));
109
    transTW->setItem(row, 1, new QTableWidgetItem(tr("Local path")));
110
    transTW->editItem(transTW->item(row, 0));
111
}
112
113
void EditTrans::on_delPB_clicked()
114
{
115
    QModelIndexList indexes = transTW->selectionModel()->selectedIndexes();
116
    vector<int> rows;
117
    for (int i = 0; i < indexes.size(); i++) {
118
  rows.push_back(indexes.at(i).row());
119
    }
120
    sort(rows.begin(), rows.end());
121
    rows.resize(unique(rows.begin(), rows.end()) - rows.begin());
122
    for (int i = rows.size()-1; i >= 0; i--) {
123
  transTW->removeRow(rows[i]);
124
    }
125
}
126
127
void EditTrans::on_transTW_itemSelectionChanged()
128
{
129
    QModelIndexList indexes = transTW->selectionModel()->selectedIndexes();
130
    if(indexes.size() < 1)
131
  delPB->setEnabled(0);
132
    else 
133
  delPB->setEnabled(1);
134
}