foreach (QAction* action, actions) {
     QToolButton* button = new QToolButton;
     QFont font = button->font();
     font.setPixelSize(14);
     button->setFont(font);
     button->setText(action->text());
     d->m_flow_layout->addWidget(button);
     QObject::connect(button, SIGNAL(clicked()), action, SLOT(trigger()));
 }
KexiDBDateTimeEdit::KexiDBDateTimeEdit(const QDateTime &datetime, QWidget *parent)
        : QWidget(parent), KexiFormDataItemInterface()
{
    m_invalidState = false;
    m_cleared = false;
    m_readOnly = false;

    m_dateEdit = new Q3DateEdit(datetime.date(), this);
    m_dateEdit->setAutoAdvance(true);
    m_dateEdit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
// m_dateEdit->setFixedWidth( QFontMetrics(m_dateEdit->font()).width("8888-88-88___") );
    connect(m_dateEdit, SIGNAL(valueChanged(QDate)), this, SLOT(slotValueChanged()));
    connect(m_dateEdit, SIGNAL(valueChanged(QDate)), this, SIGNAL(dateTimeChanged()));

    QToolButton* btn = new QToolButton(this);
    btn->setText("...");
    btn->setFixedWidth(QFontMetrics(btn->font()).width(" ... "));
    btn->setPopupDelay(1); //1 ms

    m_timeEdit = new Q3TimeEdit(datetime.time(), this);
    m_timeEdit->setAutoAdvance(true);
    m_timeEdit->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::MinimumExpanding);
    connect(m_timeEdit, SIGNAL(valueChanged(QTime)), this, SLOT(slotValueChanged()));
    connect(m_timeEdit, SIGNAL(valueChanged(QTime)), this, SIGNAL(dateTimeChanged()));

#ifdef QDateTimeEditor_HACK
    m_dte_date = KexiUtils::findFirstChild<QDateTimeEditor>(m_dateEdit, "QDateTimeEditor");
    m_dte_time = KexiUtils::findFirstChild<QDateTimeEditor>(m_timeEdit, "QDateTimeEditor");
#else
    m_dte_date = 0;
#endif

    m_datePickerPopupMenu = new QMenu(0, "date_popup");
    connect(m_datePickerPopupMenu, SIGNAL(aboutToShow()), this, SLOT(slotShowDatePicker()));
    m_datePicker = new KDatePicker(m_datePickerPopupMenu, QDate::currentDate(), 0);

    KDateTable *dt = KexiUtils::findFirstChild<KDateTable>(m_datePicker, "KDateTable");
    if (dt)
        connect(dt, SIGNAL(tableClicked()), this, SLOT(acceptDate()));
    m_datePicker->setCloseButton(true);
    m_datePicker->installEventFilter(this);
    m_datePickerPopupMenu->insertItem(m_datePicker);
    btn->setPopup(m_datePickerPopupMenu);

    QHBoxLayout* layout = new QHBoxLayout(this);
    layout->addWidget(m_dateEdit, 0);
    layout->addWidget(btn, 0);
    layout->addWidget(m_timeEdit, 0);
    //layout->addStretch(1);

    setFocusProxy(m_dateEdit);
}
 foreach (MVAbstractViewFactory* f, factories) {
     QToolButton* button = new QToolButton;
     QFont font = button->font();
     font.setPixelSize(14);
     button->setFont(font);
     button->setText(f->name());
     button->setProperty("action_name", f->id());
     d->m_flow_layout->addWidget(button);
     d->m_viewMapper->setMapping(button, f);
     QObject::connect(button, SIGNAL(clicked()), d->m_viewMapper, SLOT(map()));
     //QObject::connect(f, SIGNAL(enabledChanged(bool)), button, SLOT(setEnabled(bool)));
     d->m_buttons[f->name()] = button;
 }
Example #4
0
void QmitkToolSelectionBox::RecreateButtons()
{
  if (m_ToolManager.IsNull()) return;

  /*
  // remove all buttons that are there
  QObjectList *l = Q3ButtonGroup::queryList( "QButton" );
  QObjectListIt it( *l ); // iterate over all buttons
  QObject *obj;

  while ( (obj = it.current()) != 0 )
  {
    ++it;
    QButton* button = dynamic_cast<QButton*>(obj);
    if (button)
    {
      Q3ButtonGroup::remove(button);
      delete button;
    }
  }
  delete l; // delete the list, not the objects
  */

  // mmueller Qt impl
  QList<QAbstractButton *> l = m_ToolButtonGroup->buttons();
  // remove all buttons that are there
  QList<QAbstractButton *>::iterator it;
  QAbstractButton * btn;

  for(it=l.begin(); it!=l.end();++it)
  {
    btn = *it;
    m_ToolButtonGroup->removeButton(btn);
    //this->removeChild(btn);
    delete btn;
  }
  // end mmueller Qt impl

  mitk::ToolManager::ToolVectorTypeConst allPossibleTools = m_ToolManager->GetTools();
  mitk::ToolManager::ToolVectorTypeConst allTools;

  typedef std::pair< std::string::size_type, const mitk::Tool* > SortPairType;
  typedef std::priority_queue< SortPairType > SortedToolQueueType;
  SortedToolQueueType toolPositions;

  // clear and sort all tools
  // step one: find name/group of all tools in m_DisplayedGroups string. remember these positions for all tools.
  for ( mitk::ToolManager::ToolVectorTypeConst::const_iterator iter = allPossibleTools.begin();
        iter != allPossibleTools.end();
        ++iter)
  {
    const mitk::Tool* tool = *iter;

    std::string::size_type namePos =  m_DisplayedGroups.find( std::string("'") + tool->GetName() + "'" );
    std::string::size_type groupPos = m_DisplayedGroups.find( std::string("'") + tool->GetGroup() + "'" );

    if ( !m_DisplayedGroups.empty() && namePos == std::string::npos && groupPos == std::string::npos ) continue; // skip

    if ( m_DisplayedGroups.empty() && std::string(tool->GetName()).length() > 0 )
    {
      namePos = static_cast<std::string::size_type> (tool->GetName()[0]);
    }

    SortPairType thisPair = std::make_pair( namePos < groupPos ? namePos : groupPos, *iter );
    toolPositions.push( thisPair );
  }

  // step two: sort tools according to previously found positions in m_DisplayedGroups
  MITK_DEBUG << "Sorting order of tools (lower number --> earlier in button group)";
  while ( !toolPositions.empty() )
  {
    SortPairType thisPair = toolPositions.top();
    MITK_DEBUG << "Position " << thisPair.first << " : " << thisPair.second->GetName();

    allTools.push_back( thisPair.second );
    toolPositions.pop();
  }
  std::reverse( allTools.begin(), allTools.end() );

  MITK_DEBUG << "Sorted tools:";
  for ( mitk::ToolManager::ToolVectorTypeConst::const_iterator iter = allTools.begin();
        iter != allTools.end();
        ++iter)
  {
    MITK_DEBUG << (*iter)->GetName();
  }

  // try to change layout... bad?
  //Q3GroupBox::setColumnLayout ( m_LayoutColumns, Qt::Horizontal );
  // mmueller using gridlayout instead of Q3GroupBox
  //this->setLayout(0);
  if(m_ButtonLayout == NULL)
    m_ButtonLayout = new QGridLayout;
  /*else
    delete m_ButtonLayout;*/

  int row(0);
  int column(-1);

  int currentButtonID(0);
  m_ButtonIDForToolID.clear();
  m_ToolIDForButtonID.clear();
  QToolButton* button = 0;

  MITK_DEBUG << "Creating buttons for tools";
  // fill group box with buttons
  for ( mitk::ToolManager::ToolVectorTypeConst::const_iterator iter = allTools.begin();
        iter != allTools.end();
        ++iter)
  {
    const mitk::Tool* tool = *iter;
    int currentToolID( m_ToolManager->GetToolID( tool ) );

    ++column;
    // new line if we are at the maximum columns
    if(column == m_LayoutColumns)
    {
      ++row;
      column = 0;
    }

    button = new QToolButton;
    button->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum));
    // add new button to the group
    MITK_DEBUG << "Adding button with ID " << currentToolID;
    m_ToolButtonGroup->addButton(button, currentButtonID);
    // ... and to the layout
    MITK_DEBUG << "Adding button in row/column " << row << "/" << column;
    m_ButtonLayout->addWidget(button, row, column);

    if (m_LayoutColumns == 1)
    {
      //button->setTextPosition( QToolButton::BesideIcon );
      // mmueller
      button->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
    }
    else
    {
      //button->setTextPosition( QToolButton::BelowIcon );
      // mmueller
      button->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
    }

    //button->setToggleButton( true );
    // mmueller
    button->setCheckable ( true );

    if(currentToolID == m_ToolManager->GetActiveToolID())
      button->setChecked(true);

    QString label;
    if (m_GenerateAccelerators)
    {
      label += "&";
    }
    label += tool->GetName();
    QString tooltip = tool->GetName();
    MITK_DEBUG << tool->GetName() << ", " << label.toLocal8Bit().constData() << ", '" << tooltip.toLocal8Bit().constData();

    if ( m_ShowNames )
    {
      /*
      button->setUsesTextLabel(true);
      button->setTextLabel( label );              // a label
      QToolTip::add( button, tooltip );
      */
      // mmueller Qt
      button->setText( label );              // a label
      button->setToolTip( tooltip );
      // mmueller

      QFont currentFont = button->font();
      currentFont.setBold(false);
      button->setFont( currentFont );
    }

    us::ModuleResource iconResource = tool->GetIconResource();

    if (!iconResource.IsValid())
    {
      button->setIcon(QIcon(QPixmap(tool->GetXPM())));
    }
    else
    {
      us::ModuleResourceStream resourceStream(iconResource, std::ios::binary);
      resourceStream.seekg(0, std::ios::end);
      std::ios::pos_type length = resourceStream.tellg();
      resourceStream.seekg(0, std::ios::beg);

      char* data = new char[length];
      resourceStream.read(data, length);
      QPixmap pixmap;
      pixmap.loadFromData(QByteArray::fromRawData(data, length));
      QIcon* icon = new QIcon(pixmap);
      delete[] data;

      button->setIcon(*icon);

      if (m_ShowNames)
      {
        if (m_LayoutColumns == 1)
          button->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
        else
          button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

        button->setIconSize(QSize(24, 24));
      }
      else
      {
        button->setToolButtonStyle(Qt::ToolButtonIconOnly);
        button->setIconSize(QSize(32,32));
        button->setToolTip(tooltip);
      }
    }

    if (m_GenerateAccelerators)
    {
      QString firstLetter = QString( tool->GetName() );
      firstLetter.truncate( 1 );
      button->setShortcut( firstLetter );                      // a keyboard shortcut (just the first letter of the given name w/o any CTRL or something)
    }

    mitk::DataNode* dataNode = m_ToolManager->GetReferenceData(0);

    if (dataNode != NULL && !tool->CanHandle(dataNode->GetData()))
      button->setEnabled(false);

    m_ButtonIDForToolID[currentToolID] = currentButtonID;
    m_ToolIDForButtonID[currentButtonID] = currentToolID;

    MITK_DEBUG << "m_ButtonIDForToolID[" << currentToolID << "] == " << currentButtonID;
    MITK_DEBUG << "m_ToolIDForButtonID[" << currentButtonID << "] == " << currentToolID;

    tool->GUIProcessEventsMessage += mitk::MessageDelegate<QmitkToolSelectionBox>( this, &QmitkToolSelectionBox::OnToolGUIProcessEventsMessage ); // will never add a listener twice, so we don't have to check here
    tool->ErrorMessage += mitk::MessageDelegate1<QmitkToolSelectionBox, std::string>( this, &QmitkToolSelectionBox::OnToolErrorMessage ); // will never add a listener twice, so we don't have to check here
    tool->GeneralMessage += mitk::MessageDelegate1<QmitkToolSelectionBox, std::string>( this, &QmitkToolSelectionBox::OnGeneralToolMessage );

    ++currentButtonID;
  }
  // setting grid layout for this groupbox
  this->setLayout(m_ButtonLayout);

  //this->update();
}