code cleanup
[case] / src / case.cpp
1 // case - file manager for N900
2 // Copyright (C) 2010 Lukas Hrazky <lukkash@email.cz>
3 // 
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 // 
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 // 
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 #include "case.h"
19
20 #include <QHBoxLayout>
21 #include <QVBoxLayout>
22
23
24 Case::Case(QWidget *parent) :
25     QMainWindow(parent),
26     leftPane(new Pane(this)),
27     rightPane(new Pane(this)),
28     activePane(leftPane),
29     inactivePane(rightPane),
30     cloneBtn(new Button("clone", 0, 60, 70)),
31     swapBtn(new Button("swap", 0, 60, 70)),
32     copyBtn(new Button("copy", 0, 60, 70)),
33     moveBtn(new Button("move", 0, 60, 70)),
34     delBtn(new Button("delete", 0, 60, 70)),
35     fileOperator(new FileOperator(this))
36 {
37     QVBoxLayout *layout = new QVBoxLayout;
38     layout->setContentsMargins(0, 0, 0, 0);
39     layout->setSpacing(0);
40
41     QHBoxLayout *paneLayout = new QHBoxLayout;
42     paneLayout->setContentsMargins(0, 0, 0, 0);
43     paneLayout->setSpacing(1);
44
45     QWidget *central = new QWidget;
46     setCentralWidget(central);
47     central->setLayout(layout);
48     layout->addLayout(paneLayout);
49
50     paneLayout->addWidget(leftPane);
51     leftPane->toggleActive();
52
53     QVBoxLayout *middleButtons = new QVBoxLayout;
54     paneLayout->addLayout(middleButtons);
55     middleButtons->setSpacing(10);
56     middleButtons->setContentsMargins(0, 0, 0, 0);
57
58     cloneBtn->setContentsMargins(0, 0, 0, 0);
59
60     middleButtons->addWidget(cloneBtn);
61     middleButtons->addWidget(swapBtn);
62     middleButtons->addWidget(copyBtn);
63     middleButtons->addWidget(moveBtn);
64     middleButtons->addWidget(delBtn);
65
66     paneLayout->addWidget(rightPane);
67
68     layout->addWidget(fileOperator);
69
70     connect(leftPane, SIGNAL(switchPanes()), this, SLOT(switchActivePane()));
71     connect(rightPane, SIGNAL(switchPanes()), this, SLOT(switchActivePane()));
72
73     connect(cloneBtn, SIGNAL(pressed()), this, SLOT(clonePane()));
74     connect(swapBtn, SIGNAL(pressed()), this, SLOT(swapPanes()));
75     connect(copyBtn, SIGNAL(pressed()), this, SLOT(copyFiles()));
76     connect(moveBtn, SIGNAL(pressed()), this, SLOT(moveFiles()));
77     connect(delBtn, SIGNAL(pressed()), this, SLOT(deleteFiles()));
78 }
79
80
81 void Case::switchActivePane() {
82     Pane *tmpPane = activePane;
83     activePane = inactivePane;
84     inactivePane = tmpPane;
85
86     cloneBtn->swapIcon();
87     swapBtn->swapIcon();
88     copyBtn->swapIcon();
89     moveBtn->swapIcon();
90     delBtn->swapIcon();
91
92     activePane->toggleActive();
93     inactivePane->toggleActive();
94 }
95
96
97 void Case::keyPressEvent(QKeyEvent *e) {
98     if(e->key() == Qt::Key_H && e->modifiers() == Qt::ControlModifier) {
99         activePane->toggleShowHiddenFiles();
100     } else {
101         QMainWindow::keyPressEvent(e);
102     }
103 }
104
105
106 void Case::clonePane() {
107     inactivePane->changePath(activePane->path());
108 }
109
110
111 void Case::swapPanes() {
112     QString tmpPath = activePane->path();
113     activePane->changePath(inactivePane->path());
114     inactivePane->changePath(tmpPath);
115 }
116
117
118 void Case::copyFiles() {
119     if (activePane->selection().size()) {
120         QDir dest = inactivePane->path();
121         fileOperator->copyFiles(activePane->selection(), dest);
122     }
123 }
124
125
126 void Case::moveFiles() {
127     if (activePane->selection().size()) {
128         QDir dest = inactivePane->path();
129         fileOperator->moveFiles(activePane->selection(), dest);
130     }
131 }
132
133
134 void Case::deleteFiles() {
135     if (activePane->selection().size()) {
136         fileOperator->deleteFiles(activePane->selection());
137     }
138 }