Beispiel #1
0
void QgsDataDefinedButton::checkCheckedWidgets( bool check )
{
  // don't uncheck, only set to checked
  if ( !check )
  {
    return;
  }
  for ( int i = 0; i < mCheckedWidgets.size(); ++i )
  {
    QAbstractButton *btn = qobject_cast< QAbstractButton * >( mCheckedWidgets.at( i ) );
    if ( btn && btn->isCheckable() )
    {
      btn->setChecked( true );
      continue;
    }
    QGroupBox *grpbx = qobject_cast< QGroupBox * >( mCheckedWidgets.at( i ) );
    if ( grpbx && grpbx->isCheckable() )
    {
      grpbx->setChecked( true );
    }
  }
}
void AcceleratorManagerPrivate::manageWidget(QWidget *w, Item *item)
{
  // first treat the special cases

  QTabBar *tabBar = qobject_cast<QTabBar*>(w);
  if (tabBar)
  {
      manageTabBar(tabBar, item);
      return;
  }

  QStackedWidget *wds = qobject_cast<QStackedWidget*>( w );
  if ( wds )
  {
      QWidgetStackAccelManager::manage( wds );
      // return;
  }

  QDockWidget *dock = qobject_cast<QDockWidget*>( w );
  if ( dock )
  {
      //QWidgetStackAccelManager::manage( wds );
      manageDockWidget(dock, item);
  }


  QMenu *popupMenu = qobject_cast<QMenu*>(w);
  if (popupMenu)
  {
      // create a popup accel manager that can deal with dynamic menus
      PopupAccelManager::manage(popupMenu);
      return;
  }

  QStackedWidget *wdst = qobject_cast<QStackedWidget*>( w );
  if ( wdst )
  {
      QWidgetStackAccelManager::manage( wdst );
      // return;
  }

  QMenuBar *menuBar = qobject_cast<QMenuBar*>(w);
  if (menuBar)
  {
      manageMenuBar(menuBar, item);
      return;
  }

  if (qobject_cast<QComboBox*>(w) || qobject_cast<QLineEdit*>(w) ||
      w->inherits("Q3TextEdit") ||
      qobject_cast<QTextEdit*>(w) ||
      qobject_cast<QAbstractSpinBox*>(w) || w->inherits( "KMultiTabBar" ) )
      return;

  if ( w->inherits("KUrlRequester") ) {
    traverseChildren(w, item);
    return;
  }

  if ( qobject_cast<PathRequester*>(w) ) {
    traverseChildren(w, item);
    return;
  }

  // now treat 'ordinary' widgets
  QLabel *label =  qobject_cast<QLabel*>(w);
  if ( label  ) {
      if ( !label->buddy() )
          return;
      else {
          if ( label->textFormat() == Qt::RichText ||
               ( label->textFormat() == Qt::AutoText &&
                 Qt::mightBeRichText( label->text() ) ) )
              return;
      }
  }

  if (w->focusPolicy() != Qt::NoFocus || label || qobject_cast<QGroupBox*>(w) || qobject_cast<QRadioButton*>( w ))
  {
    QString content;
    QVariant variant;
    int tprop = w->metaObject()->indexOfProperty("text");
    if (tprop != -1)  {
        QMetaProperty p = w->metaObject()->property( tprop );
        if ( p.isValid() && p.isWritable() )
            variant = p.read (w);
        else
            tprop = -1;
    }

    if (tprop == -1)  {
        tprop = w->metaObject()->indexOfProperty("title");
        if (tprop != -1)  {
            QMetaProperty p = w->metaObject()->property( tprop );
            if ( p.isValid() && p.isWritable() )
                variant = p.read (w);
        }
    }

    if (variant.isValid())
        content = variant.toString();

    if (!content.isEmpty())
    {
        Item *i = new Item;
        i->m_widget = w;

        // put some more weight on the usual action elements
        int weight = AccelManagerAlgorithm::DEFAULT_WEIGHT;
        if (qobject_cast<QPushButton*>(w) || qobject_cast<QCheckBox*>(w) || qobject_cast<QRadioButton*>(w) || qobject_cast<QLabel*>(w))
            weight = AccelManagerAlgorithm::ACTION_ELEMENT_WEIGHT;

        // don't put weight on non-checkable group boxes,
        // as usually the contents are more important
        QGroupBox *groupBox = qobject_cast<QGroupBox*>(w);
        if (groupBox)
        {
            if (groupBox->isCheckable())
                weight = AccelManagerAlgorithm::CHECKABLE_GROUP_BOX_WEIGHT;
            else
                weight = AccelManagerAlgorithm::GROUP_BOX_WEIGHT;
        }

        i->m_content = AccelString(content, weight);
        item->addChild(i);
    }
  }
  traverseChildren(w, item);
}
void AcceleratorManagerPrivate::calculateAccelerators(Item *item, QString &used)
{
    if (!item->m_children)
        return;

    // collect the contents
    AccelStringList contents;
    for (Item *it: *item->m_children) {
        contents << it->m_content;
    }

    // find the right accelerators
    AccelManagerAlgorithm::findAccelerators(contents, used);

    // write them back into the widgets
    int cnt = -1;
    for (Item *it: *item->m_children) {
        cnt++;

        QDockWidget *dock = qobject_cast<QDockWidget*>(it->m_widget);
        if (dock) {
            if (checkChange(contents[cnt]))
                dock->setWindowTitle(contents[cnt].accelerated());
            continue;
        }
        QTabBar *tabBar = qobject_cast<QTabBar*>(it->m_widget);
        if (tabBar) {
            if (checkChange(contents[cnt]))
                tabBar->setTabText(it->m_index, contents[cnt].accelerated());
            continue;
        }
        QMenuBar *menuBar = qobject_cast<QMenuBar*>(it->m_widget);
        if (menuBar) {
            if (it->m_index >= 0) {
                QAction *maction = menuBar->actions()[it->m_index];
                if (maction) {
                    checkChange(contents[cnt]);
                    maction->setText(contents[cnt].accelerated());
                }
                continue;
            }
        }

        // we possibly reserved an accel, but we won't set it as it looks silly
        QGroupBox *groupBox = qobject_cast<QGroupBox*>(it->m_widget);
        if (groupBox && !groupBox->isCheckable())
            continue;

        int tprop = it->m_widget->metaObject()->indexOfProperty("text");
        if (tprop != -1)  {
            if (checkChange(contents[cnt]))
                it->m_widget->setProperty("text", contents[cnt].accelerated());
        } else {
            tprop = it->m_widget->metaObject()->indexOfProperty("title");
            if (tprop != -1 && checkChange(contents[cnt]))
                it->m_widget->setProperty("title", contents[cnt].accelerated());
        }
    }

    // calculate the accelerators for the children
    for (Item *it: *item->m_children) {
        if (it->m_widget && it->m_widget->isVisibleTo( item->m_widget ) )
            calculateAccelerators(it, used);
    }
}
QVariant UIPropertyGetters::DefaultGetter(QWidget* editor, SettingsPropertyMapper::WidgetType editorType, SettingsPropertyMapper::PropertyType)
{
	switch (editorType)
	{
		case SettingsPropertyMapper::CHECKBOX:
		{
			QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(editor);
			if (pCheckBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pCheckBox->isChecked());
		}
		case SettingsPropertyMapper::RADIOBUTTON:
		{
			QRadioButton* pRadioButton = qobject_cast<QRadioButton*>(editor);
			if (pRadioButton == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pRadioButton->isChecked());
		}
		case SettingsPropertyMapper::CHECKABLE_GROUPBOX:
		{
			QGroupBox* pGroupBox = qobject_cast<QGroupBox*>(editor);
			if (pGroupBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			if (!pGroupBox->isCheckable())
			{
				qCritical() << "Given QGroupBox is not checkable";
			}
			return QVariant::fromValue(pGroupBox->isChecked());
		}
		case SettingsPropertyMapper::LINE_EDIT:
		{
			QLineEdit* pLineEdit = qobject_cast<QLineEdit*>(editor);
			if (pLineEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pLineEdit->text());
		}
		case SettingsPropertyMapper::TEXT_EDIT:
		{
			QTextEdit* pTextEdit = qobject_cast<QTextEdit*>(editor);
			if (pTextEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pTextEdit->toPlainText());
		}
		case SettingsPropertyMapper::COMBOBOX:
		{
			QComboBox* pComboBox = qobject_cast<QComboBox*>(editor);
			if (pComboBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pComboBox->currentIndex());
		}
		case SettingsPropertyMapper::SPINBOX:
		{
			QSpinBox* pSpinBox = qobject_cast<QSpinBox*>(editor);
			if (pSpinBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pSpinBox->value());
		}
		case SettingsPropertyMapper::DOUBLE_SPINBOX:
		{
			QDoubleSpinBox* pDoubleSpinBox = qobject_cast<QDoubleSpinBox*>(editor);
			if (pDoubleSpinBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pDoubleSpinBox->value());
		}
		case SettingsPropertyMapper::TIME_EDIT:
		{
			QTimeEdit* pTimeEdit = qobject_cast<QTimeEdit*>(editor);
			if (pTimeEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pTimeEdit->time());
		}
		case SettingsPropertyMapper::DATETIME_EDIT:
		{
			QDateTimeEdit* pDateTimeEdit = qobject_cast<QDateTimeEdit*>(editor);
			if (pDateTimeEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return QVariant();
			}
			return QVariant::fromValue(pDateTimeEdit->dateTime());
		}
		default:
		{
			return QVariant();
		}
	}
}
void QgsPropertyOverrideButton::updateSiblingWidgets( bool state )
{

  Q_FOREACH ( const SiblingWidget &sw, mSiblingWidgets )
  {
    switch ( sw.mSiblingType )
    {

      case SiblingCheckState:
      {
        // don't uncheck, only set to checked
        if ( state )
        {
          QAbstractButton *btn = qobject_cast< QAbstractButton * >( sw.mWidgetPointer.data() );
          if ( btn && btn->isCheckable() )
          {
            btn->setChecked( sw.mNatural ? state : !state );
          }
          else
          {
            QGroupBox *grpbx = qobject_cast< QGroupBox * >( sw.mWidgetPointer.data() );
            if ( grpbx && grpbx->isCheckable() )
            {
              grpbx->setChecked( sw.mNatural ? state : !state );
            }
          }
        }
        break;
      }

      case SiblingEnableState:
      {
        QLineEdit *le = qobject_cast< QLineEdit * >( sw.mWidgetPointer.data() );
        if ( le )
          le->setReadOnly( sw.mNatural ? !state : state );
        else
          sw.mWidgetPointer.data()->setEnabled( sw.mNatural ? state : !state );
        break;
      }

      case SiblingVisibility:
      {
        sw.mWidgetPointer.data()->setVisible( sw.mNatural ? state : !state );
        break;
      }

      case SiblingExpressionText:
      {
        QLineEdit *le = qobject_cast<QLineEdit *>( sw.mWidgetPointer.data() );
        if ( le )
        {
          le->setText( mProperty.asExpression() );
        }
        else
        {
          QTextEdit *te = qobject_cast<QTextEdit *>( sw.mWidgetPointer.data() );
          if ( te )
          {
            te->setText( mProperty.asExpression() );
          }
        }
        break;
      }

      default:
        break;
    }


  }
}
void UIPropertySetters::DefaultSetter(QWidget* editor, SettingsPropertyMapper::WidgetType editorType, SettingsPropertyMapper::PropertyType propertyType, QVariant propertyValue)
{
	switch (editorType)
	{
		case SettingsPropertyMapper::CHECKBOX:
		{
			QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(editor);
			if (pCheckBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::BOOL)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pCheckBox->setChecked(propertyValue.toBool());
			break;
		}
		case SettingsPropertyMapper::RADIOBUTTON:
		{
			QRadioButton* pRadioButton = qobject_cast<QRadioButton*>(editor);
			if (pRadioButton == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::BOOL)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pRadioButton->setChecked(propertyValue.toBool());
			break;
		}
		case SettingsPropertyMapper::CHECKABLE_GROUPBOX:
		{
			QGroupBox* pGroupBox = qobject_cast<QGroupBox*>(editor);
			if (pGroupBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (!pGroupBox->isCheckable())
			{
				qCritical() << "Given QGroupBox is not checkable";
			}
			if (propertyType != SettingsPropertyMapper::BOOL)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pGroupBox->setChecked(propertyValue.toBool());
			break;
		}
		case SettingsPropertyMapper::LINE_EDIT:
		{
			QLineEdit* pLineEdit = qobject_cast<QLineEdit*>(editor);
			if (pLineEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType == SettingsPropertyMapper::STRING || propertyType == SettingsPropertyMapper::INT)
			{
				pLineEdit->setText(propertyValue.toString());
			}
			else
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
			}
			break;
		}
		case SettingsPropertyMapper::TEXT_EDIT:
		{
			QTextEdit* pTextEdit = qobject_cast<QTextEdit*>(editor);
			if (pTextEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::STRING)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pTextEdit->setText(propertyValue.toString());
			break;
		}
		case SettingsPropertyMapper::COMBOBOX:
		{
			QComboBox* pComboBox = qobject_cast<QComboBox*>(editor);
			if (pComboBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::INT)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pComboBox->setCurrentIndex(propertyValue.toInt());
			break;
		}
		case SettingsPropertyMapper::SPINBOX:
		{
			QSpinBox* pSpinBox = qobject_cast<QSpinBox*>(editor);
			if (pSpinBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::INT)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pSpinBox->setValue(propertyValue.toInt());
			break;
		}
		case SettingsPropertyMapper::DOUBLE_SPINBOX:
		{
			QDoubleSpinBox* pDoubleSpinBox = qobject_cast<QDoubleSpinBox*>(editor);
			if (pDoubleSpinBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::DOUBLE)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pDoubleSpinBox->setValue(propertyValue.toDouble());
			break;
		}
		case SettingsPropertyMapper::TIME_EDIT:
		{
			QTimeEdit* pTimeEdit = qobject_cast<QTimeEdit*>(editor);
			if (pTimeEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::TIME)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pTimeEdit->setTime(propertyValue.toTime());
			break;
		}
		case SettingsPropertyMapper::DATETIME_EDIT:
		{
			QDateTimeEdit* pDateTimeEdit = qobject_cast<QDateTimeEdit*>(editor);
			if (pDateTimeEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}
			if (propertyType != SettingsPropertyMapper::DATETIME)
			{
				qCritical() << "Unable to set value with default setter for property using value" << propertyValue;
				return;
			}
			pDateTimeEdit->setDateTime(propertyValue.toDateTime());
			break;
		}
		default: break;
	}
}
void SettingsPropertyMapper::subscribeToChanges(QWidget* editor, WidgetType editorType)
{
	switch (editorType)
	{
		case CHECKBOX:
		{
			QCheckBox* pCheckBox = qobject_cast<QCheckBox*>(editor);

			if (pCheckBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pCheckBox, SIGNAL(stateChanged(int)), SLOT(OnEditorCommit()));
			break;
		}

		case RADIOBUTTON:
		{
			QRadioButton* pRadioButton = qobject_cast<QRadioButton*>(editor);

			if (pRadioButton == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pRadioButton, SIGNAL(toggled(bool)), SLOT(OnEditorCommit()));
			break;
		}

		case CHECKABLE_GROUPBOX:
		{
			QGroupBox* pGroupBox = qobject_cast<QGroupBox*>(editor);

			if (pGroupBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			if (!pGroupBox->isCheckable())
			{
				qCritical() << "Given QGroupBox is not checkable";
			}

			connect(pGroupBox, SIGNAL(toggled(bool)), SLOT(OnEditorCommit()));
			break;
		}

		case LINE_EDIT:
		{
			QLineEdit* pLineEdit = qobject_cast<QLineEdit*>(editor);

			if (pLineEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pLineEdit, SIGNAL(textChanged(QString)), SLOT(OnEditorCommit()));
			break;
		}

		case TEXT_EDIT:
		{
			QTextEdit* pTextEdit = qobject_cast<QTextEdit*>(editor);

			if (pTextEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pTextEdit, SIGNAL(textChanged()), SLOT(OnEditorCommit()));
			break;
		}

		case COMBOBOX:
		{
			QComboBox* pComboBox = qobject_cast<QComboBox*>(editor);

			if (pComboBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pComboBox, SIGNAL(currentIndexChanged(int)), SLOT(OnEditorCommit()));
			break;
		}

		case SPINBOX:
		{
			QSpinBox* pSpinBox = qobject_cast<QSpinBox*>(editor);

			if (pSpinBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pSpinBox, SIGNAL(valueChanged(int)), SLOT(OnEditorCommit()));
			break;
		}

		case DOUBLE_SPINBOX:
		{
			QDoubleSpinBox* pDoubleSpinBox = qobject_cast<QDoubleSpinBox*>(editor);

			if (pDoubleSpinBox == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pDoubleSpinBox, SIGNAL(valueChanged(double)), SLOT(OnEditorCommit()));
			break;
		}

		case TIME_EDIT:
		{
			QTimeEdit* pTimeEdit = qobject_cast<QTimeEdit*>(editor);

			if (pTimeEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pTimeEdit, SIGNAL(timeChanged(const QTime&)), SLOT(OnEditorCommit()));
			break;
		}

		case DATETIME_EDIT:
		{
			QDateTimeEdit* pDateTimeEdit = qobject_cast<QDateTimeEdit*>(editor);

			if (pDateTimeEdit == NULL)
			{
				qCritical() << "Invalid editor given";
				return;
			}

			connect(pDateTimeEdit, SIGNAL(dateTimeChanged(const QDateTime&)), SLOT(OnEditorCommit()));
			break;
		}

		default:
			break;
	}
}
void QgsPropertyOverrideButton::updateSiblingWidgets( bool state )
{
  Q_FOREACH ( const SiblingWidget &sw, mSiblingWidgets )
  {
    switch ( sw.mSiblingType )
    {

      case SiblingCheckState:
      {
        // don't uncheck, only set to checked
        if ( state )
        {
          QAbstractButton *btn = qobject_cast< QAbstractButton * >( sw.mWidgetPointer.data() );
          if ( btn && btn->isCheckable() )
          {
            btn->setChecked( sw.mNatural ? state : !state );
          }
          else
          {
            QGroupBox *grpbx = qobject_cast< QGroupBox * >( sw.mWidgetPointer.data() );
            if ( grpbx && grpbx->isCheckable() )
            {
              grpbx->setChecked( sw.mNatural ? state : !state );
            }
          }
        }
        break;
      }

      case SiblingEnableState:
      {
        QLineEdit *le = qobject_cast< QLineEdit * >( sw.mWidgetPointer.data() );
        if ( le )
          le->setReadOnly( sw.mNatural ? !state : state );
        else
          sw.mWidgetPointer.data()->setEnabled( sw.mNatural ? state : !state );
        break;
      }

      case SiblingVisibility:
      {
        sw.mWidgetPointer.data()->setVisible( sw.mNatural ? state : !state );
        break;
      }

      case SiblingExpressionText:
      {
        QLineEdit *le = qobject_cast<QLineEdit *>( sw.mWidgetPointer.data() );
        if ( le )
        {
          le->setText( mProperty.asExpression() );
        }
        else
        {
          QTextEdit *te = qobject_cast<QTextEdit *>( sw.mWidgetPointer.data() );
          if ( te )
          {
            te->setText( mProperty.asExpression() );
          }
        }
        break;
      }

      case SiblingLinkedWidget:
      {
        if ( QgsColorButton *cb = qobject_cast< QgsColorButton * >( sw.mWidgetPointer.data() ) )
        {
          if ( state && mProperty.isProjectColor() )
          {
            QRegularExpression rx( QStringLiteral( "^project_color\\('(.*)'\\)$" ) );
            QRegularExpressionMatch match = rx.match( mExpressionString );
            if ( match.hasMatch() )
            {
              cb->linkToProjectColor( match.captured( 1 ) );
            }
          }
          else
          {
            cb->linkToProjectColor( QString() );
          }
        }
        break;
      }
    }
  }
}