OSDN Git Service

ab1896a89b82a3c7d4a5bf851ec2c7ffa85e3023
[qt-creator-jp/qt-creator-jp.git] / src / plugins / debugger / debuggertoolchaincombobox.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 "debuggertoolchaincombobox.h"
35
36 #include <projectexplorer/toolchainmanager.h>
37 #include <utils/qtcassert.h>
38
39 #include <QtCore/QFileInfo>
40 #include <QtCore/QDir>
41 #include <QtCore/QPair>
42
43 #include <QtGui/QtEvents>
44
45 typedef QPair<ProjectExplorer::Abi, QString> AbiDebuggerCommandPair;
46
47 Q_DECLARE_METATYPE(AbiDebuggerCommandPair)
48
49 namespace Debugger {
50 namespace Internal {
51
52 DebuggerToolChainComboBox::DebuggerToolChainComboBox(QWidget *parent) :
53     QComboBox(parent)
54 {
55 }
56
57 void DebuggerToolChainComboBox::init(bool hostAbiOnly)
58 {
59     const ProjectExplorer::Abi hostAbi = ProjectExplorer::Abi::hostAbi();
60     foreach (const ProjectExplorer::ToolChain *tc, ProjectExplorer::ToolChainManager::instance()->toolChains()) {
61         const ProjectExplorer::Abi abi = tc->targetAbi();
62         if (!hostAbiOnly || hostAbi.os() == abi.os()) { // Offer MSVC and Mingw, etc.
63             const QString debuggerCommand = tc->debuggerCommand();
64             if (!debuggerCommand.isEmpty()) {
65                 const AbiDebuggerCommandPair data(abi, debuggerCommand);
66                 const QString completeBase = QFileInfo(debuggerCommand).completeBaseName();
67                 const QString name = tr("%1 (%2)").arg(tc->displayName(), completeBase);
68                 addItem(name, qVariantFromValue(data));
69             }
70         }
71     }
72     setEnabled(count() > 1);
73 }
74
75 void DebuggerToolChainComboBox::setAbi(const ProjectExplorer::Abi &abi)
76 {
77     QTC_ASSERT(abi.isValid(), return; )
78     const int c = count();
79     for (int i = 0; i < c; i++) {
80         if (abiAt(i) == abi) {
81             setCurrentIndex(i);
82             break;
83         }
84     }
85 }
86
87 ProjectExplorer::Abi DebuggerToolChainComboBox::abi() const
88 {
89     return abiAt(currentIndex());
90 }
91
92 QString DebuggerToolChainComboBox::debuggerCommand() const
93 {
94     return debuggerCommandAt(currentIndex());
95 }
96
97 QString DebuggerToolChainComboBox::debuggerCommandAt(int index) const
98 {
99     if (index >= 0 && index < count()) {
100         const AbiDebuggerCommandPair abiCommandPair = qvariant_cast<AbiDebuggerCommandPair>(itemData(index));
101         return abiCommandPair.second;
102     }
103     return QString();
104 }
105
106 ProjectExplorer::Abi DebuggerToolChainComboBox::abiAt(int index) const
107 {
108     if (index >= 0 && index < count()) {
109         const AbiDebuggerCommandPair abiCommandPair = qvariant_cast<AbiDebuggerCommandPair>(itemData(index));
110         return abiCommandPair.first;
111     }
112     return ProjectExplorer::Abi();
113 }
114
115 static inline QString abiToolTip(const AbiDebuggerCommandPair &abiCommandPair)
116 {
117     QString debugger = QDir::toNativeSeparators(abiCommandPair.second);
118     debugger.replace(QString(QLatin1Char(' ')), QLatin1String("&nbsp;"));
119     return DebuggerToolChainComboBox::tr(
120                 "<html><head/><body><table><tr><td>ABI:</td><td><i>%1</i></td></tr>"
121                 "<tr><td>Debugger:</td><td>%2</td></tr>").
122             arg(abiCommandPair.first.toString(),
123                 QDir::toNativeSeparators(debugger));
124 }
125
126 bool DebuggerToolChainComboBox::event(QEvent *event)
127 {
128     if (event->type() == QEvent::ToolTip) {
129         const int index = currentIndex();
130         if (index >= 0) {
131             const AbiDebuggerCommandPair abiCommandPair = qvariant_cast<AbiDebuggerCommandPair>(itemData(index));
132             setToolTip(abiToolTip(abiCommandPair));
133         } else {
134             setToolTip(QString());
135         }
136     }
137     return QComboBox::event(event);
138 }
139
140 } // namespace Debugger
141 } // namespace Internal