diff options
author | con <qtc-committer@nokia.com> | 2010-07-13 16:42:36 +0200 |
---|---|---|
committer | con <qtc-committer@nokia.com> | 2010-07-14 14:45:18 +0200 |
commit | 352b491c87aba787fa1e87d04ea79c282f5b843d (patch) | |
tree | e7615b78d733b940488173a46261645c920e5a62 | |
parent | 29af23cdb16341e5e1d517520b0a03b91f5048b3 (diff) | |
download | qt-creator-352b491c87aba787fa1e87d04ea79c282f5b843d.tar.gz |
On Linux/Mac, show home path as ~ in recent projects.
Reviewed-by: Daniel Molkentin
-rw-r--r-- | src/libs/utils/stringutils.cpp | 19 | ||||
-rw-r--r-- | src/libs/utils/stringutils.h | 5 | ||||
-rw-r--r-- | src/plugins/coreplugin/mainwindow.cpp | 4 | ||||
-rw-r--r-- | src/plugins/projectexplorer/projectexplorer.cpp | 3 | ||||
-rw-r--r-- | src/plugins/projectexplorer/projectwelcomepagewidget.cpp | 7 | ||||
-rw-r--r-- | tests/auto/auto.pro | 3 | ||||
-rw-r--r-- | tests/auto/utils_stringutils/tst_stringutils.cpp | 76 | ||||
-rw-r--r-- | tests/auto/utils_stringutils/utils_stringutils.pro | 15 |
8 files changed, 127 insertions, 5 deletions
diff --git a/src/libs/utils/stringutils.cpp b/src/libs/utils/stringutils.cpp index cf1b4b36cf..10a2d41839 100644 --- a/src/libs/utils/stringutils.cpp +++ b/src/libs/utils/stringutils.cpp @@ -31,6 +31,8 @@ #include <QtCore/QString> #include <QtCore/QStringList> +#include <QtCore/QFileInfo> +#include <QtCore/QDir> #include <limits.h> @@ -100,4 +102,21 @@ QTCREATOR_UTILS_EXPORT QString commonPath(const QStringList &files) return common; } +QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path) +{ +#ifdef Q_OS_WIN + QString outPath = path; +#else + static const QString homePath = QDir::homePath(); + + QFileInfo fi(QDir::cleanPath(path)); + QString outPath = fi.absoluteFilePath(); + if (outPath.startsWith(homePath)) + outPath = QLatin1Char('~') + outPath.mid(homePath.size()); + else + outPath = path; +#endif + return outPath; +} + } // namespace Utils diff --git a/src/libs/utils/stringutils.h b/src/libs/utils/stringutils.h index 16cac815de..a863a61771 100644 --- a/src/libs/utils/stringutils.h +++ b/src/libs/utils/stringutils.h @@ -50,6 +50,11 @@ QTCREATOR_UTILS_EXPORT QString commonPrefix(const QStringList &strings); // "C:\foo\bar1" "C:\foo\bar2" -> "C:\foo" QTCREATOR_UTILS_EXPORT QString commonPath(const QStringList &files); +// On Linux/Mac replace user's home path with ~ +// Uses cleaned path and tries to use absolute path of "path" if possible +// If path is not sub of home path, or when running on Windows, returns the input +QTCREATOR_UTILS_EXPORT QString withTildeHomePath(const QString &path); + } // namespace Utils #endif // SETTINGSTUTILS_H diff --git a/src/plugins/coreplugin/mainwindow.cpp b/src/plugins/coreplugin/mainwindow.cpp index 4982e0ec46..e8dfa3af65 100644 --- a/src/plugins/coreplugin/mainwindow.cpp +++ b/src/plugins/coreplugin/mainwindow.cpp @@ -70,6 +70,7 @@ #include <coreplugin/settingsdatabase.h> #include <utils/pathchooser.h> #include <utils/stylehelper.h> +#include <utils/stringutils.h> #include <extensionsystem/pluginmanager.h> #include <QtCore/QDebug> @@ -1253,7 +1254,8 @@ void MainWindow::aboutToShowRecentFiles() bool hasRecentFiles = false; foreach (const QString &fileName, m_fileManager->recentFiles()) { hasRecentFiles = true; - QAction *action = aci->menu()->addAction(fileName); + QAction *action = aci->menu()->addAction( + Utils::withTildeHomePath(fileName)); action->setData(fileName); connect(action, SIGNAL(triggered()), this, SLOT(openRecentFile())); } diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index e2a925f3f1..aa6aa70e2b 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -97,6 +97,7 @@ #include <utils/consoleprocess.h> #include <utils/qtcassert.h> #include <utils/parameteraction.h> +#include <utils/stringutils.h> #include <QtCore/QtPlugin> #include <QtCore/QDateTime> @@ -1998,7 +1999,7 @@ void ProjectExplorerPlugin::updateRecentProjectMenu() const QPair<QString, QString> &s = *it; if (s.first.endsWith(QLatin1String(".qws"))) continue; - QAction *action = menu->addAction(s.first); + QAction *action = menu->addAction(Utils::withTildeHomePath(s.first)); action->setData(s.first); connect(action, SIGNAL(triggered()), this, SLOT(openRecentProject())); } diff --git a/src/plugins/projectexplorer/projectwelcomepagewidget.cpp b/src/plugins/projectexplorer/projectwelcomepagewidget.cpp index e75df35e9b..9c30289eb4 100644 --- a/src/plugins/projectexplorer/projectwelcomepagewidget.cpp +++ b/src/plugins/projectexplorer/projectwelcomepagewidget.cpp @@ -39,6 +39,8 @@ #include <coreplugin/mainwindow.h> #include <coreplugin/filemanager.h> +#include <utils/stringutils.h> + #include <QtCore/QFileInfo> #include <QtCore/QDir> #include <QtCore/QPair> @@ -146,8 +148,9 @@ void ProjectWelcomePageWidget::updateWelcomePage(const WelcomePageData &welcomeP break; const QFileInfo fi(it.first); QString label = "<b>" + it.second + - "</b><br><font color=gray>" + - fm.elidedText(it.first, Qt::ElideMiddle, 250); + "</b><br><font color=gray>" + + fm.elidedText(QDir::toNativeSeparators(Utils::withTildeHomePath(it.first)), + Qt::ElideMiddle, 250); ui->projTreeWidget->addItem(label, it.first, QDir::toNativeSeparators(fi.absolutePath())); } diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro index 9b51ff0f97..4e35b4e5c5 100644 --- a/tests/auto/auto.pro +++ b/tests/auto/auto.pro @@ -8,7 +8,8 @@ SUBDIRS += \ aggregation \ changeset \ # icheckbuild \ - generichighlighter + generichighlighter \ + utils_stringutils contains (QT_CONFIG, declarative) { SUBDIRS += qml diff --git a/tests/auto/utils_stringutils/tst_stringutils.cpp b/tests/auto/utils_stringutils/tst_stringutils.cpp new file mode 100644 index 0000000000..31990ae29b --- /dev/null +++ b/tests/auto/utils_stringutils/tst_stringutils.cpp @@ -0,0 +1,76 @@ +/************************************************************************** +** +** This file is part of Qt Creator +** +** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). +** +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** Commercial Usage +** +** Licensees holding valid Qt Commercial licenses may use this file in +** accordance with the Qt Commercial License Agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Nokia. +** +** GNU Lesser General Public License Usage +** +** 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. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at http://qt.nokia.com/contact. +** +**************************************************************************/ + +#include <stringutils.h> + +#include <QtTest/QtTest> + +class tst_StringUtils : public QObject +{ + Q_OBJECT + +private slots: + void testWithTildeHomePath(); + +}; + +void tst_StringUtils::testWithTildeHomePath() +{ +#ifndef Q_OS_WIN + // home path itself + QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QLatin1String("~")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1Char('/')), + QLatin1String("~")); + QCOMPARE(Utils::withTildeHomePath(QLatin1String("/unclean/..") + QDir::homePath()), + QLatin1String("~")); + // sub of home path + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/foo")), + QLatin1String("~/foo")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/foo/")), + QLatin1String("~/foo")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/some/path/file.txt")), + QLatin1String("~/some/path/file.txt")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/some/unclean/../path/file.txt")), + QLatin1String("~/some/path/file.txt")); + // not sub of home path + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/../foo")), + QDir::homePath() + QLatin1String("/../foo")); +#else + // windows: should return same as input + QCOMPARE(Utils::withTildeHomePath(QDir::homePath()), QDir::homePath()); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/foo")), + QDir::homePath() + QLatin1String("/foo")); + QCOMPARE(Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/../foo")), + Utils::withTildeHomePath(QDir::homePath() + QLatin1String("/../foo"))); +#endif +} + +QTEST_MAIN(tst_StringUtils) + +#include "tst_stringutils.moc" diff --git a/tests/auto/utils_stringutils/utils_stringutils.pro b/tests/auto/utils_stringutils/utils_stringutils.pro new file mode 100644 index 0000000000..3c0d6552f4 --- /dev/null +++ b/tests/auto/utils_stringutils/utils_stringutils.pro @@ -0,0 +1,15 @@ +CONFIG += qtestlib testcase +TEMPLATE = app +CONFIG -= app_bundle +DEFINES += QTCREATOR_UTILS_LIB + +UTILS_PATH = ../../../src/libs/utils + +INCLUDEPATH += $$UTILS_PATH +# Input +SOURCES += tst_stringutils.cpp \ + $$UTILS_PATH/stringutils.cpp +HEADERS += $$UTILS_PATH/stringutils.h \ + $$UTILS_PATH/utils_global.h + +TARGET=tst_$$TARGET |