summaryrefslogtreecommitdiff
path: root/src/plugins/perforce/pendingchangesdialog.cpp
blob: 84b10a8a07279c28234344310839c9ebd1c25b26 (plain)
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include "pendingchangesdialog.h"

#include "perforcetr.h"

#include <utils/layoutbuilder.h>

#include <QDialogButtonBox>
#include <QIntValidator>
#include <QListWidget>
#include <QPushButton>
#include <QRegularExpression>

namespace Perforce::Internal {

PendingChangesDialog::PendingChangesDialog(const QString &data, QWidget *parent)
    : QDialog(parent)
    , m_listWidget(new QListWidget(this))
{
    setWindowTitle(Tr::tr("P4 Pending Changes"));

    QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Cancel);
    QPushButton *submitButton = buttonBox->addButton(Tr::tr("Submit"), QDialogButtonBox::AcceptRole);
    connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
    connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);

    if (!data.isEmpty()) {
        const QRegularExpression r(QLatin1String("Change\\s(\\d+?).*?\\s\\*?pending\\*?\\s(.+?)\n"));
        QListWidgetItem *item;
        QRegularExpressionMatchIterator it = r.globalMatch(data);
        while (it.hasNext()) {
            const QRegularExpressionMatch match = it.next();
            item = new QListWidgetItem(Tr::tr("Change %1: %2").arg(match.captured(1),
                                                                   match.captured(2).trimmed()),
                                       m_listWidget);
            item->setData(Qt::UserRole, match.captured(1).trimmed());
        }
    }
    m_listWidget->setSelectionMode(QListWidget::SingleSelection);
    if (m_listWidget->count()) {
        m_listWidget->setCurrentRow(0);
        submitButton->setEnabled(true);
    } else {
        submitButton->setEnabled(false);
    }

    using namespace Layouting;

    Column {
        m_listWidget,
        buttonBox
    }.attachTo(this);

    resize(320, 250);
}

int PendingChangesDialog::changeNumber() const
{
    QListWidgetItem *item = m_listWidget->item(m_listWidget->currentRow());
    if (!item)
        return -1;
    bool ok = true;
    const int number = item->data(Qt::UserRole).toInt(&ok);
    return ok ? number : -1;
}

} // Perforce::Internal