[Ktutorial-commits] SF.net SVN: ktutorial:[297] trunk/ktutorial/ktutorial-editor
Status: Alpha
Brought to you by:
danxuliu
|
From: <dan...@us...> - 2011-04-08 15:14:25
|
Revision: 297
http://ktutorial.svn.sourceforge.net/ktutorial/?rev=297&view=rev
Author: danxuliu
Date: 2011-04-08 15:14:19 +0000 (Fri, 08 Apr 2011)
Log Message:
-----------
When a duplicated name is matched in the text completion, the name in the match list is now replaced by the unique names of the remote objects it represents.
Modified Paths:
--------------
trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.cpp
trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.h
trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.cpp
trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.h
trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameRegisterTest.cpp
trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameWidgetTest.cpp
Modified: trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.cpp 2011-04-05 02:11:06 UTC (rev 296)
+++ trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.cpp 2011-04-08 15:14:19 UTC (rev 297)
@@ -84,6 +84,24 @@
return reversedPathAsName(reversedUniquePath);
}
+QStringList RemoteObjectNameRegister::uniqueNames(const QString& name) const
+throw (DBusException) {
+ Q_ASSERT(mRemoteObjectForName.values(name).count() > 0);
+
+ QStringList uniqueNames;
+
+ if (mRemoteObjectForName.values(name).count() <= 1) {
+ uniqueNames.append(name);
+ return uniqueNames;
+ }
+
+ foreach (RemoteObject* remoteObject, mRemoteObjectForName.values(name)) {
+ uniqueNames.append(uniqueName(remoteObject));
+ }
+
+ return uniqueNames;
+}
+
RemoteObject* RemoteObjectNameRegister::findRemoteObject(const QString& name)
const {
return findRemoteObject(name, 0);
Modified: trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.h 2011-04-05 02:11:06 UTC (rev 296)
+++ trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameRegister.h 2011-04-08 15:14:19 UTC (rev 297)
@@ -38,7 +38,10 @@
* case, the names of one or more ancestors are required to differentiate
* between those remote objects. The compound name that identifies a remote
* object can be got using uniqueName(RemoteObject*). Also, given a name, the
- * remote object that it represents can be got using findObject(QString).
+ * remote object that it represents can be got using findObject(QString). An
+ * unqualified name (without any ancestor names, just the name of the remote
+ * object itself) can be used to get the unique names of all the remote objects
+ * represented by that unqualified name.
*
* Note that a name and the remote object it represents depends on the state of
* the target application. For example, if there is a dialog named
@@ -94,6 +97,17 @@
QString uniqueName(RemoteObject* remoteObject) const throw (DBusException);
/**
+ * Returns the unique names of the remote objects that have the given name.
+ * The given name can not contain any ancestor names, and it must be a
+ * registered name.
+ *
+ * @param name The name of the remote objects to get their unique names.
+ * @return The unique names of the remote objects that have the given name.
+ * @throws DBusException If a DBus error happens.
+ */
+ QStringList uniqueNames(const QString& name) const throw (DBusException);
+
+ /**
* Returns the remote object with the given name, if any.
* When the name of the desired remote object is not unique the name of some
* ancestor must be included. Ancestor names are separated using a "/". That
Modified: trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.cpp 2011-04-05 02:11:06 UTC (rev 296)
+++ trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.cpp 2011-04-08 15:14:19 UTC (rev 297)
@@ -19,12 +19,88 @@
#include "RemoteObjectNameWidget.h"
#include "ui_RemoteObjectNameWidget.h"
+#include <KDebug>
#include <KMessageBox>
#include "RemoteObjectChooser.h"
#include "RemoteObjectNameRegister.h"
#include "../targetapplication/TargetApplication.h"
+/**
+ * Completion for remote object names.
+ * The items added for completion should be the unqualified remote object names,
+ * that is, just the name of the remote objects, without any ancestor. When a
+ * match is found, and it represents more than one remote object, the match is
+ * replaced by the unique names of its remote objects.
+ */
+class RemoteObjectNameCompletion: public KCompletion {
+public:
+
+ /**
+ * Creates a new RemoteObjectNameCompletion.
+ *
+ * @param nameRegister The register to query about the remote object names.
+ */
+ RemoteObjectNameCompletion(RemoteObjectNameRegister* nameRegister):
+ KCompletion() {
+ mNameRegister = nameRegister;
+ }
+
+protected:
+
+ /**
+ * Default parent implementation.
+ * It is implemented just to avoid a compilation warning that complains that
+ * the method in the parent class is hidden by
+ * postProcessMatches(QStringList*).
+ *
+ * @param matches The matches to process.
+ */
+ virtual void postProcessMatches(KCompletionMatches* matches) const {
+ KCompletion::postProcessMatches(matches);
+ }
+
+ /**
+ * Processes the matches to provide unique names.
+ * When a match is a name that represents several remote objects that match
+ * is replaced by all the unique names of the remote objects it represents.
+ *
+ * @param matches The matches to process.
+ */
+ virtual void postProcessMatches(QStringList* matches) const {
+ QMutableListIterator<QString> it(*matches);
+ while (it.hasNext()) {
+ QStringList uniqueNames;
+
+ try {
+ uniqueNames = mNameRegister->uniqueNames(it.next());
+ } catch (DBusException e) {
+ kWarning() << "The unique names for the remote objects named "
+ << it.value() << "could not be added to the "
+ << "completion matches (" << e.message() << ")";
+ }
+
+ if (uniqueNames.count() > 1) {
+ it.remove();
+
+ qSort(uniqueNames);
+
+ foreach (const QString& uniqueName, uniqueNames) {
+ it.insert(uniqueName);
+ }
+ }
+ }
+ }
+
+private:
+
+ /**
+ * The RemoteObjectNameRegister to query about the remote object names.
+ */
+ RemoteObjectNameRegister* mNameRegister;
+
+};
+
//public:
RemoteObjectNameWidget::RemoteObjectNameWidget(QWidget* parent):
@@ -41,8 +117,10 @@
connect(ui->objectNamePushButton, SIGNAL(clicked(bool)),
this, SLOT(showRemoteObjectChooser()));
- KCompletion* completion = ui->objectNameLineEdit->completionObject();
+ KCompletion* completion =
+ new RemoteObjectNameCompletion(mRemoteObjectNameRegister);
completion->setOrder(KCompletion::Sorted);
+ ui->objectNameLineEdit->setCompletionObject(completion);
foreach (const QString& name, mRemoteObjectNameRegister->names()) {
completion->addItem(name);
Modified: trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.h
===================================================================
--- trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.h 2011-04-05 02:11:06 UTC (rev 296)
+++ trunk/ktutorial/ktutorial-editor/src/view/RemoteObjectNameWidget.h 2011-04-08 15:14:19 UTC (rev 297)
@@ -37,6 +37,8 @@
* The line edit provides text completion for the names of the remote objects
* in the target application. If there is no target application running, no
* completion is provided. As soon as it starts, the completion is enabled.
+ * When the completion matches contain a name that represents several remote
+ * objects that match is replaced by the unique name of each remote object.
*
* When a name is set in the line edit the signal
* remoteObjectChosen(RemoteObject*) is emitted.
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameRegisterTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameRegisterTest.cpp 2011-04-05 02:11:06 UTC (rev 296)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameRegisterTest.cpp 2011-04-08 15:14:19 UTC (rev 297)
@@ -58,6 +58,9 @@
void testUniqueNameWhenUniqueUnionOfGrandparentAndParent();
void testUniqueNameWhenEmptyName();
+ void testUniqueNames();
+ void testUniqueNamesWithSingleRemoteObject();
+
private:
QString mPath;
@@ -358,6 +361,42 @@
QCOMPARE(remoteObjectNameRegister.uniqueName(remoteObject), QString(""));
}
+void RemoteObjectNameRegisterTest::testUniqueNames() {
+ TargetApplication::self()->setTargetApplicationFilePath(mPath);
+ TargetApplication::self()->start();
+
+ //Give the target application time to start
+ QTest::qWait(1000);
+
+ RemoteObjectNameRegister remoteObjectNameRegister;
+
+ QStringList uniqueNames =
+ remoteObjectNameRegister.uniqueNames("Duplicated object");
+
+ QCOMPARE(uniqueNames.count(), 4);
+ QVERIFY(uniqueNames.contains("The object name 50/Duplicated object"));
+ QVERIFY(uniqueNames.contains("Duplicated grandparent/Duplicated parent/"
+ "Duplicated object"));
+ QVERIFY(uniqueNames.contains("The object name 7/Duplicated object"));
+ QVERIFY(uniqueNames.contains("The object name 8/Duplicated object"));
+}
+
+void RemoteObjectNameRegisterTest::testUniqueNamesWithSingleRemoteObject() {
+ TargetApplication::self()->setTargetApplicationFilePath(mPath);
+ TargetApplication::self()->start();
+
+ //Give the target application time to start
+ QTest::qWait(1000);
+
+ RemoteObjectNameRegister remoteObjectNameRegister;
+
+ QStringList uniqueNames =
+ remoteObjectNameRegister.uniqueNames("The object name 423");
+
+ QCOMPARE(uniqueNames.count(), 1);
+ QVERIFY(uniqueNames.contains("The object name 423"));
+}
+
/////////////////////////////////// Helpers ////////////////////////////////////
void RemoteObjectNameRegisterTest::assertNames(const QStringList& names) const {
Modified: trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameWidgetTest.cpp
===================================================================
--- trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameWidgetTest.cpp 2011-04-05 02:11:06 UTC (rev 296)
+++ trunk/ktutorial/ktutorial-editor/tests/unit/view/RemoteObjectNameWidgetTest.cpp 2011-04-08 15:14:19 UTC (rev 297)
@@ -17,6 +17,7 @@
***************************************************************************/
#include <QtTest>
+#include <QtTest/QTestKeyClicksEvent>
#define protected public
#define private public
@@ -26,6 +27,7 @@
#include <QtDBus/QtDBus>
+#include <KCompletionBox>
#include <KLineEdit>
#include <KProcess>
@@ -56,6 +58,8 @@
void testSetChosenRemoteObject();
void testSetChosenRemoteObjectWithNameNotUnique();
+ void testDuplicatedNameCompletion();
+
void testTargetApplicationStartedAfterWidget();
void testTargetApplicationStopped();
@@ -238,6 +242,34 @@
assertRemoteObjectSignal(remoteObjectChosenSpy, 2, remoteObject);
}
+void RemoteObjectNameWidgetTest::testDuplicatedNameCompletion() {
+ TargetApplication::self()->setTargetApplicationFilePath(mPath);
+ TargetApplication::self()->start();
+
+ //Give the target application time to start
+ QTest::qWait(1000);
+
+ RemoteObjectNameWidget widget;
+ KLineEdit* lineEdit = objectNameLineEdit(&widget);
+ lineEdit->setText("Duplicated ");
+
+ QTest::keyClick(lineEdit, Qt::Key_O, Qt::NoModifier, 500);
+
+ KCompletionBox* completionBox = lineEdit->completionBox();
+ QStringList completionItems = completionBox->items();
+
+ QCOMPARE(completionItems.count(), 4);
+ QCOMPARE(completionItems[0],
+ QString("Duplicated grandparent/Duplicated parent/"
+ "Duplicated object"));
+ QCOMPARE(completionItems[1],
+ QString("The object name 50/Duplicated object"));
+ QCOMPARE(completionItems[2],
+ QString("The object name 7/Duplicated object"));
+ QCOMPARE(completionItems[3],
+ QString("The object name 8/Duplicated object"));
+}
+
void RemoteObjectNameWidgetTest::testTargetApplicationStartedAfterWidget() {
RemoteObjectNameWidget widget;
QSignalSpy remoteObjectChosenSpy(&widget,
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|