ilocatorfilter.cpp 5.49 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
con's avatar
con committed
2
**
3
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
con's avatar
con committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
con's avatar
con committed
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
** 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.
23
**
hjk's avatar
hjk committed
24 25
** In addition, as a special exception, Digia gives you certain additional
** rights.  These rights are described in the Digia Qt LGPL Exception
con's avatar
con committed
26 27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
28
****************************************************************************/
hjk's avatar
hjk committed
29

30
#include "ilocatorfilter.h"
con's avatar
con committed
31

32 33 34 35 36 37
#include <QBoxLayout>
#include <QCheckBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QLabel>
#include <QLineEdit>
con's avatar
con committed
38

39
using namespace Core;
con's avatar
con committed
40

41
ILocatorFilter::ILocatorFilter(QObject *parent):
42
    QObject(parent),
hjk's avatar
hjk committed
43
    m_priority(Medium),
44
    m_includedByDefault(false),
45
    m_hidden(false),
hjk's avatar
hjk committed
46 47
    m_enabled(true),
    m_isConfigurable(true)
con's avatar
con committed
48 49 50
{
}

51
QString ILocatorFilter::shortcutString() const
con's avatar
con committed
52 53 54 55
{
    return m_shortcut;
}

56 57 58 59 60
void ILocatorFilter::prepareSearch(const QString &entry)
{
    Q_UNUSED(entry)
}

61
void ILocatorFilter::setShortcutString(const QString &shortcut)
con's avatar
con committed
62 63 64 65
{
    m_shortcut = shortcut;
}

66
QByteArray ILocatorFilter::saveState() const
con's avatar
con committed
67 68 69 70
{
    QByteArray value;
    QDataStream out(&value, QIODevice::WriteOnly);
    out << shortcutString();
71
    out << isIncludedByDefault();
con's avatar
con committed
72 73 74
    return value;
}

75
bool ILocatorFilter::restoreState(const QByteArray &state)
con's avatar
con committed
76 77 78 79 80 81 82 83 84 85 86 87 88
{
    QString shortcut;
    bool defaultFilter;

    QDataStream in(state);
    in >> shortcut;
    in >> defaultFilter;

    setShortcutString(shortcut);
    setIncludedByDefault(defaultFilter);
    return true;
}

89
bool ILocatorFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)
con's avatar
con committed
90
{
91
    Q_UNUSED(needsRefresh)
con's avatar
con committed
92 93 94 95 96 97 98 99

    QDialog dialog(parent, Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
    dialog.setWindowTitle(tr("Filter Configuration"));

    QVBoxLayout *vlayout = new QVBoxLayout(&dialog);
    QHBoxLayout *hlayout = new QHBoxLayout;
    QLineEdit *shortcutEdit = new QLineEdit(shortcutString());
    QCheckBox *limitCheck = new QCheckBox(tr("Limit to prefix"));
100
    limitCheck->setChecked(!isIncludedByDefault());
con's avatar
con committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    hlayout->addWidget(new QLabel(tr("Prefix:")));
    hlayout->addWidget(shortcutEdit);
    hlayout->addWidget(limitCheck);

    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok |
                                                       QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), &dialog, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), &dialog, SLOT(reject()));

    vlayout->addLayout(hlayout);
    vlayout->addStretch();
    vlayout->addWidget(buttonBox);

    if (dialog.exec() == QDialog::Accepted) {
        setShortcutString(shortcutEdit->text().trimmed());
        setIncludedByDefault(!limitCheck->isChecked());
        return true;
    }

    return false;
}

hjk's avatar
hjk committed
124 125 126 127
QString ILocatorFilter::trimWildcards(const QString &str)
{
    if (str.isEmpty())
        return str;
Andre Hartmann's avatar
Andre Hartmann committed
128
    int first = 0, last = str.size() - 1;
hjk's avatar
hjk committed
129 130 131 132 133 134 135 136
    const QChar asterisk = QLatin1Char('*');
    const QChar question = QLatin1Char('?');
    while (first < str.size() && (str.at(first) == asterisk || str.at(first) == question))
        ++first;
    while (last >= 0 && (str.at(last) == asterisk || str.at(last) == question))
        --last;
    if (first > last)
        return QString();
Andre Hartmann's avatar
Andre Hartmann committed
137
    return str.mid(first, last - first + 1);
hjk's avatar
hjk committed
138 139
}

140 141 142 143 144
Qt::CaseSensitivity ILocatorFilter::caseSensitivity(const QString &str)
{
    return str == str.toLower() ? Qt::CaseInsensitive : Qt::CaseSensitive;
}

145
bool ILocatorFilter::isConfigurable() const
con's avatar
con committed
146
{
hjk's avatar
hjk committed
147
    return m_isConfigurable;
con's avatar
con committed
148 149
}

150
bool ILocatorFilter::isIncludedByDefault() const
con's avatar
con committed
151
{
152
    return m_includedByDefault;
con's avatar
con committed
153 154
}

155
void ILocatorFilter::setIncludedByDefault(bool includedByDefault)
con's avatar
con committed
156
{
157 158 159
    m_includedByDefault = includedByDefault;
}

160
bool ILocatorFilter::isHidden() const
161 162 163 164
{
    return m_hidden;
}

165
void ILocatorFilter::setHidden(bool hidden)
166 167
{
    m_hidden = hidden;
con's avatar
con committed
168
}
169 170 171 172 173 174

bool ILocatorFilter::isEnabled() const
{
    return m_enabled;
}

hjk's avatar
hjk committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
Core::Id ILocatorFilter::id() const
{
    return m_id;
}

QString ILocatorFilter::displayName() const
{
    return m_displayName;
}

ILocatorFilter::Priority ILocatorFilter::priority() const
{
    return m_priority;
}

190 191 192 193
void ILocatorFilter::setEnabled(bool enabled)
{
    m_enabled = enabled;
}
hjk's avatar
hjk committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213

void ILocatorFilter::setId(Core::Id id)
{
    m_id = id;
}

void ILocatorFilter::setPriority(Priority priority)
{
    m_priority = priority;
}

void ILocatorFilter::setDisplayName(const QString &displayString)
{
    m_displayName = displayString;
}

void ILocatorFilter::setConfigurable(bool configurable)
{
    m_isConfigurable = configurable;
}