+ToolBarMenu::ToolBarMenu(QWidget* parent) :
+ KMenu(parent)
+{
+}
+
+ToolBarMenu::~ToolBarMenu()
+{
+}
+
+void ToolBarMenu::showEvent(QShowEvent* event)
+{
+ KMenu::showEvent(event);
+
+ // Adjust the position of the menu to be shown within the
+ // Dolphin window to reduce the cases that sub-menus might overlap
+ // the right screen border.
+ QPoint pos;
+ QWidget* button = parentWidget();
+ if (layoutDirection() == Qt::RightToLeft) {
+ pos = button->mapToGlobal(QPoint(0, button->height()));
+ } else {
+ pos = button->mapToGlobal(QPoint(button->width(), button->height()));
+ pos.rx() -= width();
+ }
+
+ // Assure that the menu is not shown outside the screen boundaries and
+ // that it does not overlap with the parent button.
+ const QRect screen = QApplication::desktop()->screenGeometry(QCursor::pos());
+ if (pos.x() < 0) {
+ pos.rx() = 0;
+ } else if (pos.x() + width() >= screen.width()) {
+ pos.rx() = screen.width() - width();
+ }
+
+ if (pos.y() < 0) {
+ pos.ry() = 0;
+ } else if (pos.y() + height() >= screen.height()) {
+ pos.ry() = button->mapToGlobal(QPoint(0, 0)).y() - height();
+ }
+
+ move(pos);
+}
+