logchangedialog.cpp 5.98 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
3
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
12
13
14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
18
19
20
21
22
23
24
25
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
26
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
29

30
#include "logchangedialog.h"
31
32
33
34
35
36
37
#include "gitplugin.h"
#include "gitclient.h"

#include <QTreeView>
#include <QLabel>
#include <QPushButton>
#include <QStandardItemModel>
Petar Perisin's avatar
Petar Perisin committed
38
#include <QDialogButtonBox>
39
40
#include <QItemSelectionModel>
#include <QVBoxLayout>
Petar Perisin's avatar
Petar Perisin committed
41
#include <QComboBox>
42
43
44
45
46
47
48
49
50
51
52

namespace Git {
namespace Internal {

enum Columns
{
    Sha1Column,
    SubjectColumn,
    ColumnCount
};

53
LogChangeDialog::LogChangeDialog(bool isReset, QWidget *parent)
54
55
56
57
    : QDialog(parent)
    , m_treeView(new QTreeView(this))
    , m_model(new QStandardItemModel(0, ColumnCount, this))
    , m_dialogButtonBox(new QDialogButtonBox(this))
58
    , m_resetTypeComboBox(0)
59
60
61
62
63
{
    QStringList headers;
    headers << tr("Sha1")<< tr("Subject");
    m_model->setHorizontalHeaderLabels(headers);
    QVBoxLayout *layout = new QVBoxLayout(this);
Petar Perisin's avatar
Petar Perisin committed
64
    layout->addWidget(new QLabel(tr("Reset to:"), this));
65
66
67
68
69
70
    m_treeView->setModel(m_model);
    m_treeView->setMinimumWidth(300);
    m_treeView->setUniformRowHeights(true);
    m_treeView->setRootIsDecorated(false);
    m_treeView->setSelectionBehavior(QAbstractItemView::SelectRows);
    layout->addWidget(m_treeView);
71
72
73
74
75
76
77
78
79
    QHBoxLayout *popUpLayout = new QHBoxLayout;
    if (isReset) {
        popUpLayout->addWidget(new QLabel(tr("Reset type:"), this));
        m_resetTypeComboBox = new QComboBox(this);
        m_resetTypeComboBox->addItem(tr("Hard Reset"), HardReset);
        m_resetTypeComboBox->addItem(tr("Soft Reset"), SoftReset);
        popUpLayout->addWidget(m_resetTypeComboBox);
        popUpLayout->addItem(new QSpacerItem(0, 0, QSizePolicy::Expanding, QSizePolicy::Ignored));
    }
Petar Perisin's avatar
Petar Perisin committed
80
81

    popUpLayout->addWidget(m_dialogButtonBox);
82
83
    m_dialogButtonBox->addButton(QDialogButtonBox::Cancel);
    QPushButton *okButton = m_dialogButtonBox->addButton(QDialogButtonBox::Ok);
Petar Perisin's avatar
Petar Perisin committed
84
85
    layout->addLayout(popUpLayout);

86
87
88
89
90
91
92
93
94
95
    connect(m_treeView, SIGNAL(doubleClicked(QModelIndex)),
            okButton, SLOT(animateClick()));

    connect(m_dialogButtonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_dialogButtonBox, SIGNAL(rejected()), this, SLOT(reject()));

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    resize(600, 400);
}

96
bool LogChangeDialog::runDialog(const QString &repository)
97
98
99
100
101
102
103
104
105
106
{
    if (!populateLog(repository) || !m_model->rowCount())
        return QDialog::Rejected;

    m_treeView->selectionModel()->select(m_model->index(0, 0),
                                         QItemSelectionModel::SelectCurrent | QItemSelectionModel::Rows);

    return exec() == QDialog::Accepted;
}

107
QString LogChangeDialog::commit() const
108
109
110
111
112
113
114
{
    // Return Sha1, or empty for top commit.
    if (const QStandardItem *sha1Item = currentItem(Sha1Column))
        return sha1Item->row() ? sha1Item->text() :  QString();
    return QString();
}

115
ResetType LogChangeDialog::resetType() const
Petar Perisin's avatar
Petar Perisin committed
116
{
117
118
    if (!m_resetTypeComboBox)
        return HardReset;
Petar Perisin's avatar
Petar Perisin committed
119
120
121
    return static_cast<ResetType>(m_resetTypeComboBox->itemData(m_resetTypeComboBox->currentIndex()).toInt());
}

122
bool LogChangeDialog::populateLog(const QString &repository)
123
124
125
126
{
    if (const int rowCount = m_model->rowCount())
        m_model->removeRows(0, rowCount);

127
    // Retrieve log using a custom format "Sha1:Subject [(refs)]"
128
129
    GitClient *client = GitPlugin::instance()->gitClient();
    QStringList arguments;
130
    arguments << QLatin1String("--max-count=30") << QLatin1String("--format=%h:%s %d");
131
132
133
134
135
136
137
138
139
140
    QString output;
    if (!client->synchronousLog(repository, arguments, &output))
        return false;
    foreach (const QString &line, output.split(QLatin1Char('\n'))) {
        const int colonPos = line.indexOf(QLatin1Char(':'));
        if (colonPos != -1) {
            QList<QStandardItem *> row;
            for (int c = 0; c < ColumnCount; ++c) {
                QStandardItem *item = new QStandardItem;
                item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
141
142
143
144
145
                if (line.endsWith(QLatin1Char(')'))) {
                    QFont font = item->font();
                    font.setItalic(true);
                    item->setFont(font);
                }
146
147
148
149
150
151
152
153
154
155
                row.push_back(item);
            }
            row[Sha1Column]->setText(line.left(colonPos));
            row[SubjectColumn]->setText(line.right(line.size() - colonPos - 1));
            m_model->appendRow(row);
        }
    }
    return true;
}

156
const QStandardItem *LogChangeDialog::currentItem(int column) const
157
158
159
160
161
162
163
164
165
{
    const QModelIndex currentIndex = m_treeView->selectionModel()->currentIndex();
    if (currentIndex.isValid())
        return m_model->item(currentIndex.row(), column);
    return 0;
}

} // namespace Internal
} // namespace Git