1 |
#include "droptablewidget.h" |
2 |
|
3 |
//Original dn'd from here: http://www.qtcentre.org/threads/17536-Drag-and-Drop-QTableWidget-in-UI-file |
4 |
//This constructor also initialize c++ constants (http://stackoverflow.com/questions/1423696/how-to-initialize-a-const-field-in-constructor) |
5 |
|
6 |
DropTableWidget::DropTableWidget(QWidget *parent, QBrush _disabledBackStyle, |
7 |
QBrush _disabledTextStyle) : QTableWidget(parent), |
8 |
disabledBackStyle(_disabledBackStyle), |
9 |
disabledTextStyle(_disabledTextStyle){ |
10 |
|
11 |
//set widget default properties: |
12 |
// setFrameStyle(QFrame::Sunken | QFrame::StyledPanel); |
13 |
// setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); |
14 |
setEditTriggers(QAbstractItemView::NoEditTriggers); |
15 |
setDragDropMode(QAbstractItemView::DropOnly); |
16 |
setAlternatingRowColors(true); |
17 |
// setSelectionMode(QAbstractItemView::NoSelection); |
18 |
// setShowGrid(false); |
19 |
// setWordWrap(false); |
20 |
setAcceptDrops(true); |
21 |
|
22 |
//Custom added for Vago |
23 |
setColumnCount(3); |
24 |
|
25 |
//set Header of tables |
26 |
setHorizontalHeaderLabels(QStringList()<<"File/Folder"<<"From/To"<<"Command"); |
27 |
horizontalHeader()->setStretchLastSection(true); |
28 |
} |
29 |
|
30 |
void DropTableWidget::dragEnterEvent(QDragEnterEvent *event) { |
31 |
event->acceptProposedAction(); |
32 |
emit changed(event->mimeData()); |
33 |
} |
34 |
|
35 |
void DropTableWidget::dragMoveEvent(QDragMoveEvent *event) { |
36 |
event->acceptProposedAction(); |
37 |
} |
38 |
|
39 |
void DropTableWidget::dropEvent(QDropEvent *event) { |
40 |
|
41 |
const QMimeData* mimeData = event->mimeData(); |
42 |
|
43 |
event->acceptProposedAction(); |
44 |
|
45 |
QStringList pathList = QStringList(); |
46 |
|
47 |
// check for our needed mime type, here a file or a list of files |
48 |
if (mimeData->hasUrls()) |
49 |
{ |
50 |
QList<QUrl> urlList = mimeData->urls(); |
51 |
|
52 |
// extract the local paths of the files |
53 |
for (int i = 0; i < urlList.size() && i < 2048; ++i) |
54 |
{ |
55 |
pathList.append(urlList.at(i).toLocalFile()); |
56 |
} |
57 |
} |
58 |
|
59 |
emit dropped(this, pathList); |
60 |
} |
61 |
|
62 |
void DropTableWidget::dragLeaveEvent(QDragLeaveEvent *event) { |
63 |
event->accept(); |
64 |
} |
65 |
|
66 |
void DropTableWidget::clear() { |
67 |
emit changed(); |
68 |
} |
69 |
|
70 |
//Context menu actions |
71 |
void DropTableWidget::contextMenuEvent(QContextMenuEvent *event){ |
72 |
//All the context menu is processed at the mainwindow class |
73 |
emit dtContextMenu(this,event); |
74 |
} |
75 |
|
76 |
//Custom function to swap items positions in the table |
77 |
void DropTableWidget::swapPositions(QList<int> rowsSelected, int numUnitsToMove){ |
78 |
QList<tableRowProperties> orderedList = QList<tableRowProperties>(); |
79 |
|
80 |
//Make a copy of the actual list to swap |
81 |
for(int i=0; i<this->rowCount(); i++){ |
82 |
orderedList.append(tableRowProperties()); //Add each row property (initialize) |
83 |
for(int j=0; j<this->columnCount(); j++){ |
84 |
orderedList[i].cells.append(this->item(i,j)->text()); |
85 |
|
86 |
if(this->item(i,j)->background()==this->disabledBackStyle){ //Is it disabled? |
87 |
orderedList[i].isDisabled=true; |
88 |
} |
89 |
|
90 |
} |
91 |
} |
92 |
|
93 |
//Swap the copied list for each item |
94 |
if(numUnitsToMove<0){ //if going up we need to start from the first item |
95 |
for(int i=0; i<rowsSelected.size(); i++){ |
96 |
orderedList.swap(rowsSelected.at(i),rowsSelected.at(i)+numUnitsToMove); |
97 |
} |
98 |
} |
99 |
else{ //if going down we need to start from the last item |
100 |
for(int i=rowsSelected.size()-1; i>=0; i--){ |
101 |
orderedList.swap(rowsSelected.at(i),rowsSelected.at(i)+numUnitsToMove); |
102 |
} |
103 |
} |
104 |
|
105 |
this->clear(); //clear previous selections |
106 |
|
107 |
//Switch with the ordered one |
108 |
for(int i=0; i<this->rowCount(); i++){ |
109 |
for(int j=0; j<this->columnCount(); j++){ |
110 |
QTableWidgetItem *orderedItem = new QTableWidgetItem(orderedList[i].cells.at(j)); |
111 |
this->setItem(i,j,orderedItem); |
112 |
|
113 |
if(orderedList[i].isDisabled){ //Restored disabled style |
114 |
setDisableStyleWidgetItem(orderedItem); |
115 |
} |
116 |
} |
117 |
this->updateTableToolTips(i); |
118 |
} |
119 |
|
120 |
this->clearSelection(); //clear previous selections |
121 |
|
122 |
//Select the moved rows |
123 |
this->setRangeSelected(QTableWidgetSelectionRange(rowsSelected.at(0)+numUnitsToMove,this->columnCount()-1,rowsSelected.at(rowsSelected.size()-1)+numUnitsToMove,0),true); |
124 |
//Top > top row number, Left > num colums to select to left, Bottom > bottom row number, Right > start at each column (from right) |
125 |
} |
126 |
|
127 |
//Reset a item to its initial style |
128 |
void DropTableWidget::resetStyleWidgetItem(QTableWidgetItem *currentItem){ |
129 |
if((currentItem->row()+1)%2==0){ //if the row number is par it use the alternate color scheme |
130 |
currentItem->setBackground(QPalette().brush(QPalette::Normal,QPalette::AlternateBase)); |
131 |
} |
132 |
else{ |
133 |
currentItem->setBackground(QPalette().brush(QPalette::Normal,QPalette::Base)); |
134 |
} |
135 |
currentItem->setForeground(QPalette().brush(QPalette::Normal,QPalette::WindowText)); |
136 |
} |
137 |
|
138 |
//Disable a table widget item |
139 |
void DropTableWidget::setDisableStyleWidgetItem(QTableWidgetItem *currentItem){ |
140 |
currentItem->setBackground(this->disabledBackStyle); |
141 |
currentItem->setForeground(this->disabledTextStyle); |
142 |
} |
143 |
|
144 |
QString DropTableWidget::getFileAbsolute(int row){ |
145 |
QString fileCommand=this->item(row,2)->text(); |
146 |
|
147 |
int idxFileName=fileCommand.indexOf(this->item(row,0)->text()); //Search first for the file name |
148 |
|
149 |
int fileAbsoluteStartIdx=fileCommand.lastIndexOf("\"",idxFileName); |
150 |
|
151 |
fileCommand.remove(0,fileAbsoluteStartIdx); |
152 |
|
153 |
int fileAbsoluteEndIdx=fileCommand.indexOf('"',1); //1 to find the end quote and not the start |
154 |
|
155 |
return fileCommand.remove(fileAbsoluteEndIdx+1,(fileCommand.size()-1)-fileAbsoluteEndIdx); |
156 |
} |
157 |
|
158 |
QString DropTableWidget::getOutputAbsolute(int row){ |
159 |
QString command=this->item(row,2)->text(); |
160 |
|
161 |
int fileAbsoluteEndIdx=command.indexOf("/\"",0); //let's find the /" (end of path) |
162 |
|
163 |
command.remove(fileAbsoluteEndIdx,command.size()-1); |
164 |
|
165 |
int fileAbsoluteStartIdx=command.lastIndexOf("\"",command.size()-1)+1; |
166 |
|
167 |
return command.remove(0,fileAbsoluteStartIdx); |
168 |
} |
169 |
|
170 |
|
171 |
void DropTableWidget::updateTableToolTips(int row){ |
172 |
for(int i=0; i<this->columnCount(); i++){ |
173 |
this->item(row,i)->setToolTip(this->item(row,i)->text()); |
174 |
} |
175 |
} |
176 |
|