summaryrefslogtreecommitdiff
path: root/src/libs/utils/submiteditorwidget.cpp
blob: e5069d5534f500bafc4d6509d8a74bd1c2d1fd9d (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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact:  Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the packaging
** of this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
**
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
***************************************************************************/

#include "submiteditorwidget.h"
#include "ui_submiteditorwidget.h"

#include <QtCore/QDebug>
#include <QtCore/QPointer>

enum { debug = 0 };

namespace Core {
namespace Utils {

struct SubmitEditorWidgetPrivate
{
    SubmitEditorWidgetPrivate();

    Ui::SubmitEditorWidget m_ui;
    bool m_filesSelected;
    bool m_filesChecked;
};

SubmitEditorWidgetPrivate::SubmitEditorWidgetPrivate() :
    m_filesSelected(false),
    m_filesChecked(false)
{
}

SubmitEditorWidget::SubmitEditorWidget(QWidget *parent) :
    QWidget(parent),
    m_d(new SubmitEditorWidgetPrivate)
{
    m_d->m_ui.setupUi(this);
    // File List
    m_d->m_ui.fileList->setSelectionMode(QAbstractItemView::ExtendedSelection);
    connect(m_d->m_ui.fileList, SIGNAL(itemActivated(QListWidgetItem*)), this, SLOT(triggerDiffSelected()));
    connect(m_d->m_ui.fileList, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(fileItemChanged(QListWidgetItem*)));
    connect(m_d->m_ui.fileList, SIGNAL(itemSelectionChanged()), this, SLOT(fileSelectionChanged()));

    // Text
    m_d->m_ui.description->setFont(QFont(QLatin1String("Courier")));

    setFocusPolicy(Qt::StrongFocus);
    setFocusProxy(m_d->m_ui.description);
}

SubmitEditorWidget::~SubmitEditorWidget()
{
    delete m_d;
}

void SubmitEditorWidget::registerActions(QAction *editorUndoAction,  QAction *editorRedoAction,
                         QAction *submitAction, QAction *diffAction)
{
    if (editorUndoAction) {
        editorUndoAction->setEnabled(m_d->m_ui.description->document()->isUndoAvailable());
        connect(m_d->m_ui.description, SIGNAL(undoAvailable(bool)), editorUndoAction, SLOT(setEnabled(bool)));
        connect(editorUndoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(undo()));
    }
    if (editorRedoAction) {
        editorRedoAction->setEnabled(m_d->m_ui.description->document()->isRedoAvailable());
        connect(m_d->m_ui.description, SIGNAL(redoAvailable(bool)), editorRedoAction, SLOT(setEnabled(bool)));
        connect(editorRedoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(redo()));
    }

    if (submitAction) {
        if (debug)
            qDebug() << submitAction << m_d->m_ui.fileList->count() << "items" << m_d->m_filesChecked;
        submitAction->setEnabled(m_d->m_filesChecked);
        connect(this, SIGNAL(fileCheckStateChanged(bool)), submitAction, SLOT(setEnabled(bool)));
    }
    if (diffAction) {
        if (debug)
            qDebug() << diffAction << m_d->m_filesSelected;
         diffAction->setEnabled(m_d->m_filesSelected);
         connect(this, SIGNAL(fileSelectionChanged(bool)), diffAction, SLOT(setEnabled(bool)));
         connect(diffAction, SIGNAL(triggered()), this, SLOT(triggerDiffSelected()));
    }
}

void SubmitEditorWidget::unregisterActions(QAction *editorUndoAction,  QAction *editorRedoAction,
                                           QAction *submitAction, QAction *diffAction)
{
    if (editorUndoAction) {
        disconnect(m_d->m_ui.description, SIGNAL(undoAvailableChanged(bool)), editorUndoAction, SLOT(setEnabled(bool)));
        disconnect(editorUndoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(undo()));
    }
    if (editorRedoAction) {
        disconnect(m_d->m_ui.description, SIGNAL(redoAvailableChanged(bool)), editorRedoAction, SLOT(setEnabled(bool)));
        disconnect(editorRedoAction, SIGNAL(triggered()), m_d->m_ui.description, SLOT(redo()));
    }

    if (submitAction)
        disconnect(this, SIGNAL(fileCheckStateChanged(bool)), submitAction, SLOT(setEnabled(bool)));

    if (diffAction) {
         disconnect(this, SIGNAL(fileSelectionChanged(bool)), diffAction, SLOT(setEnabled(bool)));
         disconnect(diffAction, SIGNAL(triggered()), this, SLOT(triggerDiffSelected()));
    }
}


QString SubmitEditorWidget::trimmedDescriptionText() const
{
    // Make sure we have one terminating NL
    QString text = descriptionText().trimmed();
    text += QLatin1Char('\n');
    return text;
}

QString SubmitEditorWidget::descriptionText() const
{
    return m_d->m_ui.description->toPlainText();
}

void SubmitEditorWidget::setDescriptionText(const QString &text)
{
    m_d->m_ui.description->setPlainText(text);
}

QStringList SubmitEditorWidget::fileList() const
{
    QStringList rc;
    const int count = m_d->m_ui.fileList->count();
    for (int i = 0; i < count; i++)
        rc.push_back(m_d->m_ui.fileList->item(i)->text());
    return rc;
}

void SubmitEditorWidget::addFilesUnblocked(const QStringList &list, bool checked, bool userCheckable)
{
    if (debug)
        qDebug() << Q_FUNC_INFO << list << checked << userCheckable;
    foreach (const QString &f, list) {
        QListWidgetItem *item = new QListWidgetItem(f);
        item->setCheckState(checked ? Qt::Checked : Qt::Unchecked);
        if (!userCheckable)
            item->setFlags(item->flags() & ~Qt::ItemIsUserCheckable);
        m_d->m_ui.fileList->addItem(item);
    }
}

void SubmitEditorWidget::addFiles(const QStringList &list, bool checked, bool userCheckable)
{
    if (list.empty())
        return;

    const bool blocked = m_d->m_ui.fileList->blockSignals(true);
    addFilesUnblocked(list, checked, userCheckable);
    m_d->m_ui.fileList->blockSignals(blocked);
    // Did we gain any checked files..update action accordingly
    if (!m_d->m_filesChecked && checked) {
        m_d->m_filesChecked = true;
        emit fileCheckStateChanged(m_d->m_filesChecked);
    }
}

void SubmitEditorWidget::setFileList(const QStringList &list)
{
    // Trigger enabling of menu action
    m_d->m_ui.fileList->clearSelection();

    const bool blocked = m_d->m_ui.fileList->blockSignals(true);
    m_d->m_ui.fileList->clear();
    if (!list.empty()) {
        addFilesUnblocked(list, true, true);
        // Checked files added?
        if (!m_d->m_filesChecked) {
            m_d->m_filesChecked = true;
            emit fileCheckStateChanged(m_d->m_filesChecked);
        }
    }
    m_d->m_ui.fileList->blockSignals(blocked);
}

static bool containsCheckState(const QListWidget *lw,  Qt::CheckState cs)
{
    const int count = lw->count();
    for (int i = 0; i < count; i++)
        if (lw->item(i)->checkState() == cs)
            return true;
    return false;
}

QStringList SubmitEditorWidget::selectedFiles() const
{
    QStringList rc;
    const int count = m_d->m_ui.fileList->count();
    for (int i = 0; i < count; i++) {
        const QListWidgetItem *item = m_d->m_ui.fileList->item(i);
        if (item->isSelected())
            rc.push_back(item->text());
    }
    return rc;
}

QStringList SubmitEditorWidget::checkedFiles() const
{
    QStringList rc;
    const int count = m_d->m_ui.fileList->count();
    for (int i = 0; i < count; i++) {
        const QListWidgetItem *item = m_d->m_ui.fileList->item(i);
        if (item->checkState() == Qt::Checked)
            rc.push_back(item->text());
    }
    return rc;
}

QPlainTextEdit *SubmitEditorWidget::descriptionEdit() const
{
    return m_d->m_ui.description;
}

void SubmitEditorWidget::triggerDiffSelected()
{
    const QStringList sel = selectedFiles();
    if (!sel.empty())
        emit diffSelected(sel);
}

void SubmitEditorWidget::fileItemChanged(QListWidgetItem *item)
{
    const Qt::CheckState st = item->checkState();
    if (debug)
        qDebug() << Q_FUNC_INFO << st << item->text() << m_d->m_filesChecked;
    // Enable the actions according to check state
    switch (st) {
    case Qt::Unchecked: // Item was unchecked: Any checked items left?
        if (m_d->m_filesChecked && !containsCheckState(m_d->m_ui.fileList, Qt::Checked)) {
            m_d->m_filesChecked = false;
            emit fileCheckStateChanged(m_d->m_filesChecked);
        }
        break;
    case Qt::Checked:
        // Item was Checked. First one?
        if (!m_d->m_filesChecked) {
            m_d->m_filesChecked = true;
            emit fileCheckStateChanged(m_d->m_filesChecked);
        }
        break;
    case Qt::PartiallyChecked: // Errm?
        break;
    }
}

void SubmitEditorWidget::fileSelectionChanged()
{
    const bool newFilesSelected = !m_d->m_ui.fileList->selectedItems().empty();
    if (debug)
        qDebug() << Q_FUNC_INFO << newFilesSelected;
    if (m_d->m_filesSelected != newFilesSelected) {
        m_d->m_filesSelected = newFilesSelected;
        emit fileSelectionChanged(m_d->m_filesSelected);
        if (debug)
            qDebug() << Q_FUNC_INFO << m_d->m_filesSelected;
    }
}

void SubmitEditorWidget::changeEvent(QEvent *e)
{
    switch(e->type()) {
    case QEvent::LanguageChange:
        m_d->m_ui.retranslateUi(this);
        break;
    default:
        break;
    }
}

void SubmitEditorWidget::insertTopWidget(QWidget *w)
{
    m_d->m_ui.vboxLayout->insertWidget(0, w);
}

} // namespace Utils
} // namespace Core