1 /****************************************************************************
2 * This file is part of qtFM, a simple, fast file manager.
3 * Copyright (C) 2010,2011,2012 Wittfella
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>
17 *
18 * Contact e-mail: wittfella@qtfm.org
19 *
20 ****************************************************************************/
21
22 #include <QtGui>
23 #include "tabbar.h"
24
25 //---------------------------------------------------------------------------
tabBar(QHash<QString,QIcon> * icons)26 tabBar::tabBar(QHash<QString,QIcon> * icons)
27 {
28 folderIcons = icons;
29 setAcceptDrops(1);
30 setMovable(1);
31 }
32
33 //---------------------------------------------------------------------------
mousePressEvent(QMouseEvent * event)34 void tabBar::mousePressEvent(QMouseEvent * event)
35 {
36 //middle-click to close tab
37 if(event->button() == Qt::MidButton)
38 {
39 int tab = tabAt(event->pos());
40 if(tab != -1)
41 {
42 delete history.at(tab);
43 history.removeAt(tab);
44 viewType.removeAt(tab);
45 this->removeTab(tab);
46 if (this->count()==1) { this->hide(); }
47 }
48 }
49 else
50 if(event->button() == Qt::RightButton)
51 this->setCurrentIndex(tabAt(event->pos()));
52
53 return QTabBar::mousePressEvent(event);
54 }
55
56 //---------------------------------------------------------------------------
dragEnterEvent(QDragEnterEvent * event)57 void tabBar::dragEnterEvent(QDragEnterEvent *event)
58 {
59 if(event->mimeData()->hasFormat("text/uri-list"))
60 event->acceptProposedAction();
61 }
62
63 //---------------------------------------------------------------------------
dragMoveEvent(QDragMoveEvent * event)64 void tabBar::dragMoveEvent(QDragMoveEvent *event)
65 {
66 this->setCurrentIndex(tabAt(event->pos()));
67 event->acceptProposedAction();
68 }
69
70 //---------------------------------------------------------------------------
dropEvent(QDropEvent * event)71 void tabBar::dropEvent(QDropEvent *event)
72 {
73 QList<QUrl> paths = event->mimeData()->urls();
74 QFileInfo file = QFileInfo(paths.at(0).path());
75
76 if(tabAt(event->pos()) == -1 && file.isDir()) //new tab
77 addNewTab(file.filePath(),0);
78 else
79 {
80 QStringList cutList;
81
82 //don't do anything if you drag and drop in same folder
83 if(file.canonicalPath() == tabData(currentIndex()).toString())
84 {
85 event->ignore();
86 return;
87 }
88
89 if(event->proposedAction() == 2) //cut, holding ctrl to copy is action 1
90 foreach(QUrl item, paths)
91 cutList.append(item.path());
92
93 emit dragDropTab(event->mimeData(), tabData(currentIndex()).toString(), cutList);
94 }
95
96 event->acceptProposedAction();
97 }
98
99 //---------------------------------------------------------------------------
addNewTab(QString path,int type)100 int tabBar::addNewTab(QString path, int type)
101 {
102 QFileInfo file(path);
103
104 history.append(new QStringList(path));
105 viewType.append(type);
106
107 QString filename = file.fileName();
108 if (filename.isEmpty()) { filename = "/"; }
109
110 int newtab = addTab(filename);
111 setTabData(newtab,file.filePath());
112 setIcon(newtab);
113 if (this->count()>1 && this->isHidden()) { this->show(); }
114 return newtab;
115 }
116
117 //---------------------------------------------------------------------------
setIcon(int index)118 void tabBar::setIcon(int index)
119 {
120 if(folderIcons->contains(tabText(index))) setTabIcon(index,folderIcons->value(tabText(index)));
121 else setTabIcon(index,QIcon::fromTheme("folder"));
122 }
123
124 //---------------------------------------------------------------------------
addHistory(QString path)125 void tabBar::addHistory(QString path)
126 {
127 history.at(currentIndex())->insert(0,path);
128 }
129
130 //---------------------------------------------------------------------------
remHistory()131 void tabBar::remHistory()
132 {
133 history.at(currentIndex())->removeFirst();
134 }
135
136 //---------------------------------------------------------------------------
getHistory(int index)137 QStringList *tabBar::getHistory(int index)
138 {
139 return history.value(index);
140 }
141
142 //---------------------------------------------------------------------------
getType(int index)143 int tabBar::getType(int index)
144 {
145 return viewType.at(index);
146 }
147
148 //---------------------------------------------------------------------------
setType(int type)149 void tabBar::setType(int type)
150 {
151 viewType.removeAt(currentIndex());
152 viewType.insert(currentIndex(),type);
153 }
154
155 //---------------------------------------------------------------------------
closeTab()156 void tabBar::closeTab()
157 {
158 if (currentIndex()>-1) {
159 delete history.at(currentIndex());
160 history.removeAt(currentIndex());
161 viewType.removeAt(this->currentIndex());
162 removeTab(this->currentIndex());
163 }
164 if (this->count()==1) { this->hide(); }
165 }
166