OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / help / remotehelpfilter.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "remotehelpfilter.h"
35
36 #include <QtCore/QUrl>
37
38 namespace Help {
39     namespace Internal {
40
41 RemoteFilterOptions::RemoteFilterOptions(RemoteHelpFilter *filter, QWidget *parent)
42     : QDialog(parent)
43     , m_filter(filter)
44 {
45     m_ui.setupUi(this);
46     m_ui.shortcutEdit->setText(m_filter->shortcutString());
47     m_ui.limitCheck->setChecked(!m_filter->isIncludedByDefault());
48     foreach (const QString &url, m_filter->remoteUrls()) {
49         QListWidgetItem *item = new QListWidgetItem(url);
50         m_ui.listWidget->addItem(item);
51         item->setFlags(item->flags() | Qt::ItemIsEditable);
52     }
53
54     connect(m_ui.add, SIGNAL(clicked()), this, SLOT(addNewItem()));
55     connect(m_ui.remove, SIGNAL(clicked()), this, SLOT(removeItem()));
56 }
57
58 void RemoteFilterOptions::addNewItem()
59 {
60     QListWidgetItem *item = new QListWidgetItem(tr("Double click to edit item."));
61     m_ui.listWidget->addItem(item);
62     item->setSelected(true);
63     item->setFlags(item->flags() | Qt::ItemIsEditable);
64     m_ui.listWidget->editItem(item);
65 }
66
67 void RemoteFilterOptions::removeItem()
68 {
69     if (QListWidgetItem *item = m_ui.listWidget->currentItem()) {
70         m_ui.listWidget->removeItemWidget(item);
71         delete item;
72     }
73 }
74
75 // -- RemoteHelpFilter
76
77 RemoteHelpFilter::RemoteHelpFilter()
78 {
79     setIncludedByDefault(false);
80     setShortcutString(QLatin1String("r"));
81     m_remoteUrls.append(QLatin1String("http://www.bing.com/search?q=%1"));
82     m_remoteUrls.append(QLatin1String("http://www.google.com/search?q=%1"));
83     m_remoteUrls.append(QLatin1String("http://search.yahoo.com/search?p=%1"));
84     m_remoteUrls.append(QLatin1String("http://www.cplusplus.com/reference/stl/%1"));
85     m_remoteUrls.append(QLatin1String("http://en.wikipedia.org/w/index.php?search=%1"));
86 }
87
88 RemoteHelpFilter::~RemoteHelpFilter()
89 {
90 }
91
92 QString RemoteHelpFilter::displayName() const
93 {
94     return tr("Online Documentation");
95 }
96
97 QString RemoteHelpFilter::id() const
98 {
99     return QLatin1String("RemoteHelpFilter");
100 }
101
102 Locator::ILocatorFilter::Priority RemoteHelpFilter::priority() const
103 {
104     return Medium;
105 }
106
107 QList<Locator::FilterEntry> RemoteHelpFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &pattern)
108 {
109     QList<Locator::FilterEntry> entries;
110     foreach (const QString &url, m_remoteUrls) {
111         if (future.isCanceled())
112             break;
113
114         entries.append(Locator::FilterEntry(this, url.arg(pattern), QVariant(),
115             m_icon));
116     }
117     return entries;
118 }
119
120 void RemoteHelpFilter::accept(Locator::FilterEntry selection) const
121 {
122     const QString &url = selection.displayName;
123     if (!url.isEmpty()) {
124         emit linkActivated(url);
125     }
126 }
127
128 void RemoteHelpFilter::refresh(QFutureInterface<void> &future)
129 {
130     Q_UNUSED(future)
131     // Nothing to refresh
132 }
133
134 QByteArray RemoteHelpFilter::saveState() const
135 {
136     QByteArray value;
137     QDataStream out(&value, QIODevice::WriteOnly);
138     out << m_remoteUrls.join(QLatin1String("^"));
139     out << shortcutString();
140     out << isIncludedByDefault();
141     return value;
142 }
143
144 bool RemoteHelpFilter::restoreState(const QByteArray &state)
145 {
146     QDataStream in(state);
147
148     QString value;
149     in >> value;
150     m_remoteUrls = value.split(QLatin1String("^"), QString::SkipEmptyParts);
151
152     QString shortcut;
153     in >> shortcut;
154     setShortcutString(shortcut);
155
156     bool defaultFilter;
157     in >> defaultFilter;
158     setIncludedByDefault(defaultFilter);
159
160     return true;
161 }
162
163 bool RemoteHelpFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)
164 {
165     Q_UNUSED(needsRefresh)
166     RemoteFilterOptions optionsDialog(this, parent);
167     if (optionsDialog.exec() == QDialog::Accepted) {
168         m_remoteUrls.clear();
169         setIncludedByDefault(!optionsDialog.m_ui.limitCheck->isChecked());
170         setShortcutString(optionsDialog.m_ui.shortcutEdit->text().trimmed());
171         for (int i = 0; i < optionsDialog.m_ui.listWidget->count(); ++i)
172             m_remoteUrls.append(optionsDialog.m_ui.listWidget->item(i)->text());
173         return true;
174     }
175     return true;
176 }
177
178     } // namespace Internal
179 } // namespace Help