This repository was archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWorkingTreeItemView.cpp
More file actions
168 lines (130 loc) · 4.69 KB
/
WorkingTreeItemView.cpp
File metadata and controls
168 lines (130 loc) · 4.69 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* MacGitver
* Copyright (C) 2012 Sascha Cunz <sascha@babbelbox.org>
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License (Version 2) as published by the Free Software Foundation.
*
* 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "WorkingTreeItemView.h"
#include "libMacGitverCore/Config/Config.h"
#include "libMacGitverCore/Widgets/HeaderView.h"
#include "libGitWrap/Index.hpp"
#include "libGitWrap/Result.hpp"
#include "WorkingTreeAbstractItem.h"
#include "WorkingTreeModel.h"
#include <QAbstractProxyModel>
#include <QMessageBox>
WorkingTreeItemView::WorkingTreeItemView(QWidget *parent)
: TreeViewCtxMenu( parent )
, mHeader( new HeaderView( Qt::Horizontal ) )
, mModel( 0 )
{
#ifdef Q_OS_MACX
setAttribute( Qt::WA_MacShowFocusRect, false );
#endif
setHeader( mHeader );
mHeader->setConfigName( QLatin1String( "Worktree/Columns" ) );
WorkingTreeCtxMenu::setupActions( this );
connect( this, SIGNAL(contextMenu(QModelIndex, QPoint)),
this, SLOT(contextMenu(QModelIndex, QPoint)) );
}
void WorkingTreeItemView::setModel(QAbstractItemModel *model)
{
TreeViewCtxMenu::setModel( model );
QAbstractProxyModel *apm = qobject_cast< QAbstractProxyModel* >( model );
while ( apm )
{
model = apm->sourceModel();
Q_ASSERT( model );
apm = qobject_cast< QAbstractProxyModel* >( model );
}
mModel = qobject_cast< WorkingTreeModel * >( model );
}
void WorkingTreeItemView::onWtCtxStage()
{
Heaven::Action* action = qobject_cast< Heaven::Action* >( sender() );
if ( !action )
return;
QModelIndex srcIndex = deeplyMapToSource( currentIndex() );
if ( !srcIndex.isValid() )
return;
WorkingTreeAbstractItem* item = mModel->indexToItem( srcIndex );
Q_ASSERT( item );
Git::Repository repo = mModel->repository();
Git::Result r;
Git::Index i = repo.index( r );
i.read( r );
i.addFile( r, item->path() );
i.write( r );
// TODO: workaround to update the model
mModel->setRepository(repo);
if ( !r )
{
QMessageBox::warning( this, trUtf8("Error while adding file to Git index"),
trUtf8("File not staged. Git message:\n%1").arg(r.errorText()) );
}
}
void WorkingTreeItemView::onWtCtxDiscard()
{
int button = QMessageBox::question( this, trUtf8("Discard unstaged changes"),
trUtf8("Discard unstaged changes?\n\n"
"You cannot undo this action."),
QMessageBox::Ok | QMessageBox::Abort );
if ( button != QMessageBox::Ok )
return;
Heaven::Action* action = qobject_cast< Heaven::Action* >( sender() );
if ( !action )
return;
QModelIndex srcIndex = deeplyMapToSource( currentIndex() );
if ( !srcIndex.isValid() )
return;
WorkingTreeAbstractItem* item = mModel->indexToItem( srcIndex );
Q_ASSERT( item );
Git::Repository repo = mModel->repository();
Git::Result r;
Git::Index i = repo.index( r );
// index stays unchanged; no need to read/write
i.checkoutFiles( r, QStringList( item->path() ) );
// TODO: workaround to update the model
mModel->setRepository(repo);
if ( !r )
{
QMessageBox::warning( this, trUtf8("Error while discarding file changes"),
trUtf8("File remains unchanged. Git message:\n%1").arg(r.errorText()) );
}
}
void WorkingTreeItemView::contextMenu( const QModelIndex& index, const QPoint& globalPos )
{
Q_ASSERT( mModel );
QModelIndex srcIndex = deeplyMapToSource( index );
if ( !srcIndex.isValid() )
return;
WorkingTreeAbstractItem* item = mModel->indexToItem( srcIndex );
Heaven::Menu* menu = 0;
if ( item && !item->isDirectory() )
{
menu = menuCtxMenuWtFile;
//menu->setActivationContext( item );
}
if ( menu )
menu->showPopup( globalPos );
}
QModelIndex WorkingTreeItemView::deeplyMapToSource( QModelIndex current ) const
{
while( current.isValid() )
{
const QAbstractProxyModel* apm = qobject_cast< const QAbstractProxyModel* >( current.model() );
if( !apm )
return current;
current = apm->mapToSource( current );
}
return QModelIndex();
}