Kiln » TortoiseHg » TortoiseHg
Clone URL:  
filelistview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# Copyright (c) 2009-2010 LOGILAB S.A. (Paris, FRANCE). # http://www.logilab.fr/ -- mailto:contact@logilab.fr # # 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 the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. import os from tortoisehg.util import hglib from tortoisehg.hgqt.i18n import _ from tortoisehg.hgqt import qtlib, visdiff from PyQt4.QtCore import * from PyQt4.QtGui import * class HgFileListView(QTableView): """ A QTableView for displaying a HgFileListModel """ fileSelected = pyqtSignal(QString, QString) linkActivated = pyqtSignal(QString) clearDisplay = pyqtSignal() def __init__(self, repo, parent, multiselectable): QTableView.__init__(self, parent) self.repo = repo self.multiselectable = multiselectable self.setShowGrid(False) self.horizontalHeader().hide() self.verticalHeader().hide() self.verticalHeader().setDefaultSectionSize(20) if multiselectable: self.setSelectionMode(QAbstractItemView.ExtendedSelection) else: self.setSelectionMode(QAbstractItemView.SingleSelection) self.setSelectionBehavior(QAbstractItemView.SelectRows) self.setTextElideMode(Qt.ElideLeft) def setModel(self, model): QTableView.setModel(self, model) model.layoutChanged.connect(self.layoutChanged) self.selectionModel().currentRowChanged.connect(self.onRowChange) self.horizontalHeader().setResizeMode(1, QHeaderView.Stretch) def setRepo(self, repo): self.repo = repo def setContext(self, ctx): self.ctx = ctx self.model().setContext(ctx) def currentFile(self): index = self.currentIndex() return self.model().fileFromIndex(index) def getSelectedFiles(self): model = self.model() sf = [model.fileFromIndex(eachIndex) for eachIndex in self.selectedRows()] return sf def layoutChanged(self): 'file model has new contents' index = self.currentIndex() count = len(self.model()) if index.row() == -1: # index is changing, onRowChange() called for us self.selectRow(0) elif index.row() >= count: if count: # index is changing, onRowChange() called for us self.selectRow(count-1) else: self.clearDisplay.emit() else: # redisplay previous row self.onRowChange(index) def onRowChange(self, index, *args): if index is None: index = self.currentIndex() data = self.model().dataFromIndex(index) if data: self.fileSelected.emit(hglib.tounicode(data['path']), data['status']) else: self.clearDisplay.emit() def resizeEvent(self, event): if self.model() is not None: vp_width = self.viewport().width() col_widths = [self.columnWidth(i) \ for i in range(1, self.model().columnCount())] col_width = vp_width - sum(col_widths) col_width = max(col_width, 50) self.setColumnWidth(0, col_width) QTableView.resizeEvent(self, event) # ## Mouse drag # def selectedRows(self): return self.selectionModel().selectedRows() def dragObject(self): if type(self.ctx.rev()) == str: return paths = [] for index in self.selectedRows(): paths.append(self.model().fileFromIndex(index)) if not paths: return if self.ctx.rev() is None: base = self.repo.root else: base, _ = visdiff.snapshot(self.repo, paths, self.ctx) urls = [] for path in paths: urls.append(QUrl.fromLocalFile(os.path.join(base, path))) if urls: d = QDrag(self) m = QMimeData() m.setUrls(urls) d.setMimeData(m) d.start(Qt.CopyAction) def mousePressEvent(self, event): self.pressPos = event.pos() self.pressTime = QTime.currentTime() return QTableView.mousePressEvent(self, event) def mouseMoveEvent(self, event): d = event.pos() - self.pressPos if d.manhattanLength() < QApplication.startDragDistance(): return QTableView.mouseMoveEvent(self, event) elapsed = self.pressTime.msecsTo(QTime.currentTime()) if elapsed < QApplication.startDragTime(): return QTableView.mouseMoveEvent(self, event) self.dragObject() return QTableView.mouseMoveEvent(self, event)