~dpm/ubuntu-filemanager-app/merge-plugin-fix-ap

« back to all changes in this revision

Viewing changes to test_folderlistmodel/simpleUI/simplelist.cpp

  • Committer: David Planella
  • Date: 2014-03-29 08:42:46 UTC
  • mfrom: (0.1.47 plugin)
  • Revision ID: david.planella@ubuntu.com-20140329084246-lv88jt2dfttn60em
Initial merge of the folderlistmodel plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************
 
2
 *
 
3
 * Copyright 2013 Canonical Ltd.
 
4
 * Copyright 2013 Carlos J Mazieri <carlos.mazieri@gmail.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU Lesser General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 *
 
18
 * File: simplelist.cpp
 
19
 * Date: 3/9/2013
 
20
 */
 
21
 
 
22
#include "simplelist.h"
 
23
#include "ui_simplelist.h"
 
24
#include "dirmodel.h"
 
25
 
 
26
#include <QDir>
 
27
#include <QMetaType>
 
28
#include <QHeaderView>
 
29
#include <QDebug>
 
30
#include <QProgressBar>
 
31
#include <QMessageBox>
 
32
#include <QTimer>
 
33
 
 
34
SimpleList::SimpleList(QWidget *parent) :
 
35
    QWidget(parent),
 
36
    ui(new Ui::SimpleList),
 
37
    m_curRow(-1),
 
38
    m_pbar( new QProgressBar() )
 
39
{
 
40
    ui->setupUi(this);
 
41
 
 
42
    m_model = new DirModel(this);
 
43
 
 
44
    DirModel::registerMetaTypes();
 
45
    ui->tableView->setModel(m_model);   
 
46
 
 
47
    connect(ui->tableView, SIGNAL(clicked(QModelIndex)),
 
48
            this,          SLOT(onRowClicked(QModelIndex)));
 
49
    connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)),
 
50
            this,          SLOT(onOpenItem(QModelIndex)));
 
51
 
 
52
    connect(ui->tableView->verticalHeader(), SIGNAL(sectionClicked(int)),
 
53
            this,                            SLOT(onVerticalHeaderClicked(int)));
 
54
 
 
55
    connect(m_model, SIGNAL(pathChanged(QString)),
 
56
            this,    SLOT(pathChanged(QString)));
 
57
 
 
58
    connect(ui->pushButtonCdUp,   SIGNAL(clicked()),  this, SLOT(onCdUP()));
 
59
    connect(ui->pushButtonCopy,   SIGNAL(clicked()),  this, SLOT(onCopy()));
 
60
    connect(ui->pushButtonCut,    SIGNAL(clicked()),  this, SLOT(onCut()));
 
61
    connect(ui->pushButtonDelete, SIGNAL(clicked()),  this, SLOT(onRemove()));
 
62
    connect(ui->pushButtonGoHome, SIGNAL(clicked()),  this, SLOT(onGoHome()));
 
63
    connect(ui->pushButtonIntoDirs,SIGNAL(clicked()), this, SLOT(onCdInto()));
 
64
    connect(ui->pushButtonNewDir, SIGNAL(clicked()),  this, SLOT(onNewDir()));
 
65
    connect(ui->pushButtonPaste,  SIGNAL(clicked()),  this, SLOT(onPaste()));
 
66
    connect(ui->pushButtonRename, SIGNAL(clicked()),  this, SLOT(onRename()));
 
67
 
 
68
    connect(ui->checkBoxShowDirs,    SIGNAL(clicked(bool)), this, SLOT(onShowDirs(bool)));
 
69
    connect(ui->checkBoxShowHidden,  SIGNAL(clicked(bool)), this, SLOT(onShowHidden(bool)));
 
70
    connect(ui->checkBoxExtFsWatcher, SIGNAL(toggled(bool)),this, SLOT(onExtFsWatcherEnabled(bool)));
 
71
 
 
72
    connect(ui->pushButtonOpen,   SIGNAL(clicked()),
 
73
            this,                 SLOT(onOpen()));
 
74
 
 
75
    connect(ui->lineEditOpen,   SIGNAL(returnPressed()),
 
76
            this,                 SLOT(onOpen()));
 
77
 
 
78
    ui->checkBoxShowDirs->setChecked( m_model->showDirectories() );
 
79
 
 
80
    resize(800,600);
 
81
 
 
82
    connect(m_model, SIGNAL(insertedRow(int)),
 
83
            this,    SLOT(resizeColumnForName(int)));
 
84
 
 
85
    connect(ui->tableView->horizontalHeader(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)),
 
86
            this,                              SLOT(setSort(int,Qt::SortOrder)));
 
87
 
 
88
    connect(m_model, SIGNAL(progress(int,int,int)),
 
89
            this,    SLOT(progress(int,int,int)));
 
90
 
 
91
    connect(m_model, SIGNAL(clipboardChanged()),
 
92
            this,    SLOT(clipboardChanged()));
 
93
 
 
94
    connect(m_model, SIGNAL(error(QString,QString)),
 
95
            this,    SLOT(error(QString,QString)));
 
96
 
 
97
    ui->tableView->horizontalHeader()->setSortIndicator(0,Qt::AscendingOrder);
 
98
 
 
99
    m_pbar->setMaximum(100);
 
100
    m_pbar->setMinimum(0);
 
101
 
 
102
    m_model->setReadsMediaMetadata(true);
 
103
    ui->checkBoxExtFsWatcher->click();
 
104
    m_model->goHome();
 
105
 
 
106
    clipboardChanged();
 
107
}
 
108
 
 
109
SimpleList::~SimpleList()
 
110
{
 
111
    delete ui;
 
112
}
 
113
 
 
114
void SimpleList::onRowClicked(QModelIndex index)
 
115
{
 
116
    if (index.isValid())
 
117
    {
 
118
        m_curRow = index.row();
 
119
    }
 
120
    else
 
121
    {
 
122
        m_curRow = -1;
 
123
    }
 
124
}
 
125
 
 
126
 
 
127
void SimpleList::onCdInto()
 
128
{
 
129
    m_model->cdIntoIndex(m_curRow);
 
130
}
 
131
 
 
132
void SimpleList::onGoHome()
 
133
{
 
134
     m_model->goHome();
 
135
}
 
136
 
 
137
void SimpleList::onCdUP()
 
138
{
 
139
    m_model->cdUp();
 
140
}
 
141
 
 
142
void SimpleList::onRemove()
 
143
{
 
144
     m_model->removeIndex(m_curRow);
 
145
}
 
146
 
 
147
void SimpleList::onCopy()
 
148
{
 
149
    m_model->copyIndex(m_curRow);
 
150
}
 
151
 
 
152
void SimpleList::onCut()
 
153
{
 
154
    m_model->cutIndex(m_curRow);
 
155
}
 
156
 
 
157
 
 
158
void SimpleList::onPaste()
 
159
{
 
160
    m_model->paste();
 
161
}
 
162
 
 
163
void SimpleList::onNewDir()
 
164
{
 
165
   m_model->mkdir(ui->lineEditNewDir->text());
 
166
}
 
167
 
 
168
void SimpleList::onRename()
 
169
{
 
170
   m_model->rename(m_curRow, ui->lineEditRename->text());
 
171
}
 
172
 
 
173
void SimpleList::onShowDirs(bool show)
 
174
{
 
175
    m_model->setShowDirectories(show);
 
176
}
 
177
 
 
178
void SimpleList::onShowHidden(bool s)
 
179
{
 
180
    m_model->setShowHiddenFiles(s);
 
181
}
 
182
 
 
183
void SimpleList::onVerticalHeaderClicked(int row)
 
184
{
 
185
    m_curRow = row;
 
186
}
 
187
 
 
188
 
 
189
void SimpleList::setSort(int col, Qt::SortOrder order)
 
190
{
 
191
    if (col == 0 || col == 2)
 
192
    {
 
193
        if (col == 0)
 
194
        {
 
195
            m_model->setSortBy(DirModel::SortByName);
 
196
        }
 
197
        else
 
198
        {
 
199
            m_model->setSortBy(DirModel::SortByDate);
 
200
        }
 
201
        DirModel::SortOrder o = (DirModel::SortOrder)order;
 
202
        m_model->setSortOrder(o);
 
203
    }
 
204
}
 
205
 
 
206
void SimpleList::clipboardChanged()
 
207
{   
 
208
    ui->clipboardNumber->setText( QString::number(m_model->getClipboardUrlsCounter()));
 
209
}
 
210
 
 
211
void SimpleList::progress(int cur, int total, int percent)
 
212
{
 
213
    QString p;
 
214
    m_pbar->setValue(percent);
 
215
    if (cur == 0 && percent == 0)
 
216
    {
 
217
        m_pbar->reset();
 
218
        m_pbar->show();
 
219
    }
 
220
    else
 
221
        if (percent == 100)
 
222
        {
 
223
            QTimer::singleShot(200, m_pbar, SLOT(hide()));
 
224
        }
 
225
    p.sprintf("progress(cur=%d, total=%d, percent=%d)", cur,total,percent);
 
226
    qDebug() << p;
 
227
}
 
228
 
 
229
 
 
230
void SimpleList::error(QString title, QString message)
 
231
{
 
232
    if (m_pbar)
 
233
    {
 
234
        m_pbar->hide();
 
235
    }
 
236
    QMessageBox::critical(this, title, message);
 
237
}
 
238
 
 
239
void SimpleList::onOpenItem(QModelIndex index)
 
240
{
 
241
    if (index.isValid())
 
242
    {
 
243
        m_curRow = index.row();
 
244
        if (!m_model->openIndex(m_curRow))
 
245
        {
 
246
            QModelIndex idx = m_model->index(m_curRow, 0);
 
247
            QString item = m_model->data(idx).toString();
 
248
            error("Could not open item index", item);
 
249
        }
 
250
    }
 
251
    else
 
252
    {
 
253
        m_curRow = -1;
 
254
    }
 
255
}
 
256
 
 
257
 
 
258
void SimpleList::pathChanged(QString path)
 
259
{
 
260
    this->setWindowTitle(path);
 
261
}
 
262
 
 
263
void SimpleList::resizeColumnForName(int)
 
264
{
 
265
    ui->tableView->resizeColumnToContents(0);
 
266
}
 
267
 
 
268
 
 
269
void SimpleList::onOpen()
 
270
{
 
271
    if ( ! m_model->openPath(ui->lineEditOpen->text()) )
 
272
    {
 
273
          QMessageBox::critical(this, "DirModel::openIndex() failed to open" , ui->lineEditOpen->text());
 
274
    }
 
275
}
 
276
 
 
277
 
 
278
void SimpleList::onExtFsWatcherEnabled(bool enable)
 
279
{
 
280
    m_model->setEnabledExternalFSWatcher(enable);
 
281
}