Make tooltip positioning work

This commit is contained in:
Sebastian Kügler 2013-04-09 00:08:23 +02:00
parent 5a82aa9211
commit 9eda3882ca
7 changed files with 29 additions and 81 deletions

View File

@ -40,10 +40,10 @@ import org.kde.plasma.extras 2.0 as PlasmaExtras
MouseArea {
id: tooltip
property string mainText: tooltipMaintext.text // string
property string subText: tooltipSubtext.text // string
property string iconSource: tooltipIcon.source // icon name
property string image: tooltipImage.source // string / url to the image
property string mainText // string
property string subText // string
property string iconSource // icon name
property string image // string / url to the image
property Item target: parent
property alias mainItem: tooltipWindow.mainItem

View File

@ -132,12 +132,12 @@ void DialogProxy::setVisualParent(QQuickItem *visualParent)
return;
}
if (visualParent) {
setPosition(popupPosition(visualParent, Qt::AlignCenter));
}
m_visualParent = visualParent;
emit visualParentChanged();
if (visualParent && isVisible()) {
setPosition(popupPosition(visualParent, Qt::AlignCenter));
}
}
bool DialogProxy::isVisible() const
@ -147,9 +147,8 @@ bool DialogProxy::isVisible() const
void DialogProxy::setVisible(const bool visible)
{
//qDebug() << visible;
QRect avail = QRect(400, 300, 1200, 800); // FIXME
//QRect avail = QRect(400, 300, 1200, 800); // FIXME
QRect avail;
if (visible) {
syncToMainItemSize();
if (!m_visualParent.isNull()) {
@ -194,15 +193,13 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
//If no item was specified try to align at the center of the parent view
QQuickItem *parentItem = qobject_cast<QQuickItem *>(parent());
if (parentItem && parentItem->window()) {
//qDebug() << "NO visual parent ... Centering at " << (parentItem->window()->geometry().center() - QPoint(width()/2, height()/2));
return parentItem->window()->geometry().center() - QPoint(width()/2, height()/2);
} else {
//qDebug() << "No QQuickItem as parent found";
return QPoint();
}
}
QPointF pos = item->mapToScene(QPointF(0, 0));
//qDebug() << "I've an Item at " << pos;
if (item->window() && item->window()->screen()) {
pos = item->window()->mapToGlobal(pos.toPoint());
} else {
@ -282,7 +279,6 @@ QPoint DialogProxy::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignment)
} else {
menuPos.setY(avail.height() - item->boundingRect().height() + bottomMargin);
}
//qDebug() << menuPos;
}
//qDebug() << "Popup position" << menuPos << " Location: Plasma::" <<locString(l);
@ -362,10 +358,11 @@ void DialogProxy::syncMainItemToSize()
void DialogProxy::syncToMainItemSize()
{
qDebug() << "XXXX XXX size sync";
if (!m_mainItem) {
return;
}
//qDebug() << " main item: " << m_mainItem.
//FIXME: workaround to prevent dialogs of Popup type disappearing on the second show
const QSize s = QSize(m_mainItem.data()->width(), m_mainItem.data()->height()) +
QSize(m_frameSvgItem->margins()->left() + m_frameSvgItem->margins()->right(),

View File

@ -150,7 +150,7 @@ Q_SIGNALS:
void locationChanged();
void visualParentChanged();
private Q_SLOTS:
public Q_SLOTS:
void syncMainItemToSize();
void syncToMainItemSize();

View File

@ -102,7 +102,14 @@ void ToolTip::setMainItem(QQuickItem *mainItem)
QQuickItem *ToolTip::visualParent() const
{
return m_visualParent.data();
if (m_visualParent.data()) {
qDebug() << "returning real visualParent";
return m_visualParent.data();
} else {
qDebug() << "returning parent as visualParent";
QQuickItem *qqi = qobject_cast<QQuickItem*>(parent());
return qqi;
}
}
void ToolTip::setVisualParent(QQuickItem *visualParent)
@ -126,19 +133,22 @@ bool ToolTip::isVisible() const
void ToolTip::setVisible(const bool visible)
{
ToolTipDialog *dlg = ToolTipDialog::instance();
qDebug() << visible;
qDebug() << "creating tooltipdialog" << visible;
if (visible) {
//dlg->mainItem()->deleteLater();
// QObject *myObject = m_mainComponent.data()->create();
// QQuickItem *item = qobject_cast<QQuickItem*>(myObject);
qDebug() << "XXX Setting visible: " << mainItem();
qDebug() << "---- XXX Setting visible: " << mainItem();
setMainItem(mainItem());
qDebug() << "XXX showing tooltip: " << ToolTipDialog::instance();
qDebug() << "XXX positioning near: " << visualParent()->objectName();
dlg->setMainItem(mainItem());
dlg->setVisualParent(m_visualParent.data());
dlg->setPosition(dlg->popupPosition(visualParent()));
dlg->setVisualParent(visualParent());
dlg->syncToMainItemSize();
//dlg->setPosition(dlg->popupPosition(visualParent()));
dlg->setVisible(true);
// syncGeometry();

View File

@ -31,7 +31,6 @@ ToolTipDialog::ToolTipDialog(QQuickItem *parent)
{
setFlags(Qt::ToolTip);
m_frameSvgItem->setImagePath("widgets/tooltip");
qDebug() << "XXX TOolTip ctor'ed";
}
ToolTipDialog::~ToolTipDialog()

View File

@ -42,11 +42,6 @@ ToolTipWindow::ToolTipWindow(QWindow *parent)
m_syncTimer->setSingleShot(true);
m_syncTimer->setInterval(250);
connect(m_syncTimer, &QTimer::timeout, this, &ToolTipWindow::syncGeometry);
connect(this, SIGNAL(targetChanged()), this, SLOT(updateToolTip()));
connect(this, SIGNAL(mainTextChanged()), this, SLOT(updateToolTip()));
connect(this, SIGNAL(subTextChanged()), this, SLOT(updateToolTip()));
connect(this, SIGNAL(imageChanged()), this, SLOT(updateToolTip()));
}
ToolTipWindow::~ToolTipWindow()
@ -76,8 +71,6 @@ void ToolTipWindow::setTarget(QQuickItem *target)
void ToolTipWindow::syncGeometry()
{
qDebug() << " XXX synching geometry";
qDebug() << "XXXX mainitem : " << mainItem()->width() << mainItem()->height();
resize(mainItem()->width(), mainItem()->height());
setPosition(popupPosition(visualParent()));
@ -132,47 +125,6 @@ void ToolTipWindow::setImage(QVariant name)
emit imageChanged();
}
void ToolTipWindow::updateToolTip()
{
if (!m_widget) {
return;
}
// Plasma::ToolTipContent data;
// data.setMainText(m_mainText);
// data.setSubText(m_subText);
// set image
switch (m_image.type()) {
case QVariant::String: {
QString name = m_image.toString();
if (!name.isEmpty()) {
QIcon icon = QIcon::fromTheme(name);
if (!icon.isNull()) {
// data.setImage(icon.pixmap(IconSize(KIconLoader::Desktop)));
}
}
break;
}
case QVariant::Icon: {
QIcon icon = m_image.value<QIcon>();
// data.setImage(icon);
break;
}
case QVariant::Pixmap: {
QPixmap pixmap = m_image.value<QPixmap>();
// data.setImage(pixmap);
break;
}
default:
break;
}
//Plasma::ToolTipManager::self()->setContent(m_widget, data);
}
QQuickItem *ToolTipWindow::mainItem() const
{
return m_mainItem.data();
@ -180,11 +132,7 @@ QQuickItem *ToolTipWindow::mainItem() const
void ToolTipWindow::setMainItem(QQuickItem *mainItem)
{
qDebug() << "XXXX mainitem changed: " << mainItem->width() << mainItem->height();
// resize(400, 200);
if (m_mainItem.data() != mainItem) {
qDebug() << " XXX new mainItem";
disconnect(m_mainItem.data(), &QQuickItem::widthChanged, this, &ToolTipWindow::syncGeometry);
disconnect(m_mainItem.data(), &QQuickItem::heightChanged, this, &ToolTipWindow::syncGeometry);
if (m_mainItem) {
@ -194,7 +142,6 @@ void ToolTipWindow::setMainItem(QQuickItem *mainItem)
if (mainItem) {
//mainItem->setParentItem(0);
connect(m_mainItem.data(), &QQuickItem::widthChanged, this, &ToolTipWindow::syncGeometry);
connect(m_mainItem.data(), &QQuickItem::heightChanged, this, &ToolTipWindow::syncGeometry);
qDebug() << "XXX new mainITem connected";
@ -209,7 +156,7 @@ void ToolTipWindow::setMainItem(QQuickItem *mainItem)
}
}
//if this is called in Compenent.onCompleted we have to wait a loop the item is added to a scene
//if this is called in Component.onCompleted we have to wait a loop the item is added to a scene
emit mainItemChanged();
}
}
@ -230,7 +177,6 @@ void ToolTipWindow::setVisualParent(QQuickItem *visualParent)
}
disconnect(m_visualParent.data(), &QQuickItem::xChanged, this, &ToolTipWindow::syncGeometry);
disconnect(m_visualParent.data(), &QQuickItem::yChanged, this, &ToolTipWindow::syncGeometry);
//disconnect(visualParent, &QQuickItem::yChanged, this, &ToolTipWindow::syncGeometry);
m_visualParent = visualParent;
connect(m_visualParent.data(), &QQuickItem::xChanged, this, &ToolTipWindow::syncGeometry);
@ -242,14 +188,12 @@ void ToolTipWindow::setVisualParent(QQuickItem *visualParent)
bool ToolTipWindow::isVisible() const
{
return QQuickWindow::isVisible();
}
void ToolTipWindow::setVisible(const bool visible)
{
qDebug() << visible;
if (visible) {
//setPosition(popupPosition());
syncGeometry();
raise();
}
@ -266,7 +210,6 @@ QPoint ToolTipWindow::popupPosition(QQuickItem *item, Qt::AlignmentFlag alignmen
QPointF itemScreenPos;
QPointF pos = item->mapToScene(QPointF(0, 0));
//qDebug() << "I've an Item at " << pos;
if (item->window() && item->window()->screen()) {
pos = item->window()->mapToGlobal(pos.toPoint());
} else {

View File

@ -113,7 +113,6 @@ Q_SIGNALS:
protected Q_SLOTS:
void syncGeometry();
void updateToolTip();
private:
QTimer *m_syncTimer;