summaryrefslogtreecommitdiffstats
path: root/ui/qt/widgets/path_selection_edit.cpp
blob: 660a6f53f9910e46f887b8710867fc53ec69fe54 (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
/* path_chooser_delegate.cpp
 * Delegate to select a file path for a treeview entry
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "config.h"

#include "ui/util.h"

#include <ui/qt/widgets/path_selection_edit.h>
#include "ui/qt/widgets/wireshark_file_dialog.h"
#include "ui/qt/utils/qt_ui_utils.h"

#include <QHBoxLayout>
#include <QToolButton>
#include <QWidget>
#include <QLineEdit>

PathSelectionEdit::PathSelectionEdit(QString title, QString path, bool selectFile, QWidget *parent) :
    QWidget(parent)
{
    _title = title;
    _path = path;
    _selectFile = selectFile;

    _edit = new QLineEdit(this);
    _edit->setText(_path);
    connect(_edit, &QLineEdit::textChanged, this, &PathSelectionEdit::setPath);

    _button = new QToolButton(this);
    _button->setText(tr("Browse"));
    connect(_button, &QToolButton::clicked, this, &PathSelectionEdit::browseForPath);

    setContentsMargins(0, 0, 0, 0);
    QHBoxLayout *hbox = new QHBoxLayout(this);
    hbox->setContentsMargins(0, 0, 0, 0);
    hbox->addWidget(_edit);
    hbox->addWidget(_button);
    hbox->setSizeConstraint(QLayout::SetMinimumSize);

    setLayout(hbox);

    setFocusProxy(_edit);
    setFocusPolicy(_edit->focusPolicy());
}

PathSelectionEdit::PathSelectionEdit(QWidget *parent) :
    PathSelectionEdit(tr("Select a path"), QString(), true, parent)
{}

void PathSelectionEdit::setPath(QString newPath)
{
    _path = newPath;
    if (!sender()) {
        _edit->blockSignals(true);
        _edit->setText(newPath);
        _edit->blockSignals(false);
    } else {
        emit pathChanged(newPath);
    }
}

QString PathSelectionEdit::path() const
{
    return _path;
}

void PathSelectionEdit::browseForPath()
{
    QString openDir = _path;

    if (openDir.isEmpty()) {
        openDir = openDialogInitialDir();
    }

    QString newPath;
    if ( _selectFile )
        newPath = WiresharkFileDialog::getOpenFileName(this, _title, openDir);
    else
        newPath = WiresharkFileDialog::getExistingDirectory(this, _title, openDir);

    if (!newPath.isEmpty()) {
        _edit->setText(newPath);
    }
}