add drag and drop to AppletInfo

add tests for AppletInfo drag and drop support

svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=467639
This commit is contained in:
Aaron J. Seigo 2005-10-05 20:44:14 +00:00
parent c8758daa20
commit af483fda80
4 changed files with 77 additions and 1 deletions

View File

@ -19,7 +19,10 @@ the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA. Boston, MA 02110-1301, USA.
******************************************************************/ ******************************************************************/
#include <qfileinfo.h> #include <QBuffer>
#include <QFileInfo>
#include <QMimeData>
#include <kdesktopfile.h> #include <kdesktopfile.h>
#include <kapplication.h> #include <kapplication.h>
@ -210,4 +213,36 @@ bool AppletInfo::operator<=(const AppletInfo& rhs) const
return name().lower() <= rhs.name().lower(); return name().lower() <= rhs.name().lower();
} }
void AppletInfo::populateMimeData(QMimeData* mimeData)
{
QByteArray a;
QDataStream s(&a, QIODevice::WriteOnly);
s << desktopFilePath();
mimeData->setData("application/plasmaAppletInfo", a);
}
bool AppletInfo::canDecode(const QMimeData* mimeData)
{
return mimeData->hasFormat("application/plasmaAppletInfo");
}
AppletInfo AppletInfo::fromMimeData(const QMimeData* mimeData)
{
QByteArray a = mimeData->data("application/plasmaAppletInfo");
if (a.isEmpty())
{
return AppletInfo();
}
QBuffer buff(&a);
buff.open(QIODevice::ReadOnly);
QDataStream s(&buff);
QString desktopFile;
s >> desktopFile;
AppletInfo info(desktopFile);
return info;
}
} // Plasma namespace } // Plasma namespace

View File

@ -145,6 +145,35 @@ class KDE_EXPORT AppletInfo
*/ */
bool operator==(const AppletInfo& rhs) const; bool operator==(const AppletInfo& rhs) const;
/**
* Part of the drag and drop support for AppletInfo.
* Populates a QMimeData object with the necessary data to create an
* equivalent AppletInfo object on the receiving end.
* @param mimeData a pointer to a QMimeData object. Any Applet Info
* previously added to this object will be overwritten.
*/
void populateMimeData(QMimeData* mimeData);
/**
* Part of the drag and drop support for AppletInfo.
* Returns true if the QMimeData object passed in contains the proper
* mimetype for an AppletInfo
* @param mimeData a pointer to a QMimeData object which may contain
* AppletInfo data
*/
static bool canDecode(const QMimeData* mimeData);
/**
* Part of the drag and drop support for AppletInfo.
* Given a QMimeData object that contains an AppletInfo, returns a
* corresponding AppletInfo object
* @param mimeData a pointer to a QMimeData object which should contain
* AppletInfo data.
* @see canDecode
*/
static AppletInfo fromMimeData(const QMimeData* mimeData);
private: private:
void setName(const QString& name); void setName(const QString& name);
void setComment(const QString& comment); void setComment(const QString& comment);

View File

@ -19,6 +19,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
#include <QDir> #include <QDir>
#include <QMimeData>
#include <kaboutdata.h> #include <kaboutdata.h>
#include <kinstance.h> #include <kinstance.h>
@ -245,5 +246,15 @@ void TestAppletInfo::copyConstructor()
COMPARE(copied.hidden(), notUniqueNative->hidden()); COMPARE(copied.hidden(), notUniqueNative->hidden());
} }
void TestAppletInfo::dragAndDrop()
{
QMimeData* data = new QMimeData;
notUniqueNative->populateMimeData(data);
VERIFY(Plasma::AppletInfo::canDecode(data));
Plasma::AppletInfo dropped = Plasma::AppletInfo::fromMimeData(data);
COMPARE(dropped.desktopFile(), notUniqueNative->desktopFile());
delete data;
}
QTTEST_MAIN(TestAppletInfo) QTTEST_MAIN(TestAppletInfo)
#include "testAppletInfo.moc" #include "testAppletInfo.moc"

View File

@ -58,6 +58,7 @@ class TestAppletInfo: public QObject
void hidden(); void hidden();
void assignment(); void assignment();
void copyConstructor(); void copyConstructor();
void dragAndDrop();
private: private:
Plasma::AppletInfo* notUniqueNative; Plasma::AppletInfo* notUniqueNative;