kurllistrequester.cpp 5.81 KB
Newer Older
1 2
/*****************************************************************************
 * Copyright (C) 2005 Csaba Karai <krusader@users.sourceforge.net>           *
Davide Gianforte's avatar
Davide Gianforte committed
3
 * Copyright (C) 2005-2020 Krusader Krew [https://krusader.org]              *
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *                                                                           *
 * This file is part of Krusader [https://krusader.org].                     *
 *                                                                           *
 * Krusader is free software: you can redistribute it and/or modify          *
 * it under the terms of the GNU General Public License as published by      *
 * the Free Software Foundation, either version 2 of the License, or         *
 * (at your option) any later version.                                       *
 *                                                                           *
 * Krusader 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 Krusader.  If not, see [http://www.gnu.org/licenses/].         *
 *****************************************************************************/
Csaba Karai's avatar
 
Csaba Karai committed
20 21

#include "kurllistrequester.h"
22
#include "../FileSystem/filesystem.h"
23
#include "../icon.h"
24

25 26 27 28 29 30 31 32 33
// QtGui
#include <QPixmap>
#include <QCursor>
#include <QKeyEvent>
// QtWidgets
#include <QFileDialog>
#include <QLayout>
#include <QGridLayout>
#include <QMenu>
34

Simon Persson's avatar
Simon Persson committed
35
#include <KI18n/KLocalizedString>
36
#include <KWidgetsAddons/KMessageBox>
Csaba Karai's avatar
 
Csaba Karai committed
37 38 39

#define DELETE_ITEM_ID    100

40 41
KURLListRequester::KURLListRequester(Mode requestMode, QWidget *parent)
    : QWidget(parent), mode(requestMode)
Csaba Karai's avatar
 
Csaba Karai committed
42
{
Fathi Boudra's avatar
Fathi Boudra committed
43 44
    // Creating the widget

45
    auto *urlListRequesterGrid = new QGridLayout(this);
Fathi Boudra's avatar
Fathi Boudra committed
46 47 48 49 50 51 52 53
    urlListRequesterGrid->setSpacing(0);
    urlListRequesterGrid->setContentsMargins(0, 0, 0, 0);

    urlLineEdit = new KLineEdit(this);
    urlListRequesterGrid->addWidget(urlLineEdit, 0, 0);

    urlAddBtn = new QToolButton(this);
    urlAddBtn->setText("");
54
    urlAddBtn->setIcon(Icon("arrow-down"));
Fathi Boudra's avatar
Fathi Boudra committed
55 56 57 58
    urlListRequesterGrid->addWidget(urlAddBtn, 0, 1);

    urlBrowseBtn = new QToolButton(this);
    urlBrowseBtn->setText("");
59
    urlBrowseBtn->setIcon(Icon("folder"));
Fathi Boudra's avatar
Fathi Boudra committed
60 61
    urlListRequesterGrid->addWidget(urlBrowseBtn, 0, 2);

62 63 64 65
    urlListBox = new KrListWidget(this);
    urlListBox->setSelectionMode(QAbstractItemView::ExtendedSelection);
    urlListRequesterGrid->addWidget(urlListBox, 1, 0, 1, 3);

Fathi Boudra's avatar
Fathi Boudra committed
66 67 68 69 70 71 72
    // add shell completion

    completion.setMode(KUrlCompletion::FileCompletion);
    urlLineEdit->setCompletionObject(&completion);

    // connection table

73 74 75 76 77
    connect(urlAddBtn, &QToolButton::clicked, this, &KURLListRequester::slotAdd);
    connect(urlBrowseBtn, &QToolButton::clicked, this, &KURLListRequester::slotBrowse);
    connect(urlLineEdit, &KLineEdit::returnPressed, this, &KURLListRequester::slotAdd);
    connect(urlListBox, &KrListWidget::itemRightClicked, this, &KURLListRequester::slotRightClicked);
    connect(urlLineEdit, &KLineEdit::textChanged, this, &KURLListRequester::changed);
Csaba Karai's avatar
 
Csaba Karai committed
78 79 80 81
}

void KURLListRequester::slotAdd()
{
Fathi Boudra's avatar
Fathi Boudra committed
82 83
    QString text = urlLineEdit->text().simplified();
    if (text.length()) {
Fathi Boudra's avatar
Fathi Boudra committed
84
        QString error;
Fathi Boudra's avatar
Fathi Boudra committed
85 86 87 88 89 90 91 92 93
        emit checkValidity(text, error);

        if (!error.isNull())
            KMessageBox::error(this, error);
        else {
            urlListBox->addItem(text);
            urlLineEdit->clear();
            emit changed();
        }
Csaba Karai's avatar
 
Csaba Karai committed
94
    }
Csaba Karai's avatar
 
Csaba Karai committed
95 96 97 98
}

void KURLListRequester::slotBrowse()
{
99
    QUrl url;
100 101
    switch (mode) {
        case RequestFiles:
102
            url = QFileDialog::getOpenFileUrl(this);
103 104
            break;
        case RequestDirs:
105
            url = QFileDialog::getExistingDirectoryUrl(this);
106 107
            break;
    }
Fathi Boudra's avatar
Fathi Boudra committed
108
    if (!url.isEmpty())
109
        urlLineEdit->setText(url.toDisplayString(QUrl::PreferLocalFile));
Fathi Boudra's avatar
Fathi Boudra committed
110
    urlLineEdit->setFocus();
Csaba Karai's avatar
 
Csaba Karai committed
111 112 113 114
}

void KURLListRequester::keyPressEvent(QKeyEvent *e)
{
Fathi Boudra's avatar
Fathi Boudra committed
115 116 117 118 119
    if (e->key() == Qt::Key_Delete) {
        if (urlListBox->hasFocus()) {
            deleteSelectedItems();
            return;
        }
Csaba Karai's avatar
 
Csaba Karai committed
120 121
    }

Fathi Boudra's avatar
Fathi Boudra committed
122
    QWidget::keyPressEvent(e);
Csaba Karai's avatar
 
Csaba Karai committed
123 124 125 126
}

void KURLListRequester::deleteSelectedItems()
{
Alexander Lohnau's avatar
Alexander Lohnau committed
127
    const QList<QListWidgetItem *> selectedItems = urlListBox->selectedItems();
128 129
    for (QListWidgetItem *item : selectedItems)
        delete item;
Fathi Boudra's avatar
Fathi Boudra committed
130
    emit changed();
Csaba Karai's avatar
 
Csaba Karai committed
131 132
}

Fathi Boudra's avatar
Fathi Boudra committed
133
void KURLListRequester::slotRightClicked(QListWidgetItem *item, const QPoint &pos)
Csaba Karai's avatar
 
Csaba Karai committed
134
{
135
    if (item == nullptr)
Fathi Boudra's avatar
Fathi Boudra committed
136 137
        return;

Simon Persson's avatar
Simon Persson committed
138
    QMenu popupMenu(this);
Fathi Boudra's avatar
Fathi Boudra committed
139 140 141 142 143 144 145 146 147
    QAction * menuAction = popupMenu.addAction(i18n("Delete"));

    if (menuAction == popupMenu.exec(pos)) {
        if (item->isSelected())
            deleteSelectedItems();
        else {
            delete item;
            emit changed();
        }
148
    }
Csaba Karai's avatar
 
Csaba Karai committed
149 150
}

151
QList<QUrl> KURLListRequester::urlList()
Csaba Karai's avatar
 
Csaba Karai committed
152
{
153
    QStringList lines;
Fathi Boudra's avatar
Fathi Boudra committed
154

155 156 157
    const QString lineEditText = urlLineEdit->text().simplified();
    if (!lineEditText.isEmpty()) {
        lines.append(lineEditText);
Fathi Boudra's avatar
Fathi Boudra committed
158 159 160 161
    }

    for (int i = 0; i != urlListBox->count(); i++) {
        QListWidgetItem *item = urlListBox->item(i);
162 163
        lines.append(item->text().simplified());
    }
Fathi Boudra's avatar
Fathi Boudra committed
164

165 166
    QList<QUrl> urls;
    for (QString text : lines) {
Fathi Boudra's avatar
Fathi Boudra committed
167
        QString error;
Fathi Boudra's avatar
Fathi Boudra committed
168 169
        emit checkValidity(text, error);
        if (error.isNull())
170
            urls.append(QUrl::fromUserInput(text, QString(), QUrl::AssumeLocalFile));
Fathi Boudra's avatar
Fathi Boudra committed
171 172 173
    }

    return urls;
Csaba Karai's avatar
 
Csaba Karai committed
174 175
}

176
void KURLListRequester::setUrlList(const QList<QUrl> &urlList)
Csaba Karai's avatar
 
Csaba Karai committed
177
{
Fathi Boudra's avatar
Fathi Boudra committed
178 179 180
    urlLineEdit->clear();
    urlListBox->clear();

181
    for (const QUrl& url : urlList) {
182 183
        urlListBox->addItem(url.toDisplayString(QUrl::PreferLocalFile));
    }
184

Fathi Boudra's avatar
Fathi Boudra committed
185
    emit changed();
Csaba Karai's avatar
 
Csaba Karai committed
186 187
}