Exemplo n.º 1
0
void BlDateSearch::s_searchFecha()
{
    BL_FUNC_DEBUG

    QDialog *diag = new QDialog ( 0 );
    diag->setModal ( true );
    QCalendarWidget *calend = new QCalendarWidget ( diag );
    /// Se pone el 1er dia del calendario a lunes.
    calend->setFirstDayOfWeek ( Qt::Monday );

    /// Evitar fechas demasiado antiguas
    calend->setMinimumDate( QDate ( 1900, 1, 1 ) );

    /// Si el campo estaba vacío, seleccionar una fecha imposible, pero mostrar el mes actual
    if ( m_textoFecha->text().isEmpty() ) {
        calend->setSelectedDate ( calend->minimumDate() );
        calend->setCurrentPage ( QDate::currentDate().year(), QDate::currentDate().month() );
    }

    /// Si ya hay una fecha en el campo, abrir el calendario con ese día seleccionado inicialmente
    else {
        calend->setSelectedDate ( blNormalizeDate ( m_textoFecha->text() ) );
    }

    connect ( calend, SIGNAL ( activated ( const QDate & ) ), diag, SLOT ( accept() ) );

    /// Creamos un layout donde estara el contenido de la ventana y la ajustamos al QDialog
    /// para que sea redimensionable y aparezca el titulo de la ventana.
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget ( calend );
    layout->setMargin ( 0 );
    layout->setSpacing ( 0 );
    diag->setLayout ( layout );
    diag->setWindowTitle ( _ ( "Seleccione fecha" ) );
    diag->exec();

    /// Si la fecha es imposible, significa que el usuario no ha seleccionado una fecha
    /// y su campo debe quedarse como estaba: vacío
    if ( calend->selectedDate() != QDate ( 1900, 1, 1 ) ) {
        m_textoFecha->setText ( calend->selectedDate().toString ( "dd/MM/yyyy" ) );
    }

    /// Liberamos la memoria
    delete layout;
    delete calend;
    delete diag;

    emit ( valueChanged ( m_textoFecha->text() ) );
    emit ( editingFinished () );

    
}
Exemplo n.º 2
0
void QgsAttributeEditor::selectDate()
{
  QPushButton *pb = qobject_cast<QPushButton *>( sender() );
  if ( !pb )
    return;

  QWidget *hbox = qobject_cast<QWidget *>( pb->parent() );
  if ( !hbox )
    return;

  QLineEdit *le = hbox->findChild<QLineEdit *>();
  if ( !le )
    return;

  QDialog *dlg = new QDialog();
  dlg->setWindowTitle( tr( "Select a date" ) );
  QVBoxLayout *vl = new QVBoxLayout( dlg );

  QCalendarWidget *cw = new QCalendarWidget( dlg );
  cw->setSelectedDate( QDate::fromString( le->text(), Qt::ISODate ) );
  vl->addWidget( cw );

  QDialogButtonBox *buttonBox = new QDialogButtonBox( dlg );
  buttonBox->addButton( QDialogButtonBox::Ok );
  buttonBox->addButton( QDialogButtonBox::Cancel );
  vl->addWidget( buttonBox );

  connect( buttonBox, SIGNAL( accepted() ), dlg, SLOT( accept() ) );
  connect( buttonBox, SIGNAL( rejected() ), dlg, SLOT( reject() ) );

  if ( dlg->exec() == QDialog::Accepted )
  {
    le->setText( cw->selectedDate().toString( Qt::ISODate ) );
  }
}
Exemplo n.º 3
0
QDate QtopiaInputDialog::getDate(QWidget *parent, const QString &title, const QString &label, const QDate &date,
                                 const QDate &minDate, const QDate &maxDate, bool *ok)
{
#ifdef CALENDAR_FOR_DATE
    QCalendarWidget *cal = new QCalendarWidget();
    cal->setSelectedDate(date);
    cal->setMinimumDate(minDate);
    cal->setMaximumDate(maxDate);
    cal->setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);
    QTextCharFormat headerFormat = cal->headerTextFormat();
    headerFormat.setBackground(QApplication::palette().window());
    headerFormat.setForeground(QApplication::palette().windowText());
    cal->setHeaderTextFormat(headerFormat);
    QWidget *navBar = cal->findChild<QWidget*>("qt_calendar_navigationbar");
    if (navBar)
        navBar->setBackgroundRole(QPalette::Window);

    QtopiaInputDialog dlg(parent, title, label, cal);
    bool accepted = (QtopiaApplication::execDialog(&dlg) == QDialog::Accepted);
    if (ok)
        *ok = accepted;
    return cal->selectedDate();
#else
    QDateEdit *de = new QDateEdit(date);
    de->setMinimumDate(minDate);
    de->setMaximumDate(maxDate);

    QtopiaInputDialog dlg(parent, title, label, de);
    bool accepted = (QtopiaApplication::execDialog(&dlg) == QDialog::Accepted);
    if (ok)
        *ok = accepted;
    return de->date();
#endif
}
Exemplo n.º 4
0
int main(int argc, char **argv)
{
    QApplication app(argc, argv);

    QCalendarWidget calendar;
    calendar.setSelectedDate(calendar.selectedDate().addDays(3));
    calendar.setGridVisible(true);
    calendar.show();

    QCalendarWidget calendarMin;
    calendarMin.setMinimumDate(calendarMin.selectedDate().addDays(-7));
    calendarMin.setGridVisible(true);
    calendarMin.show();

    QCalendarWidget calendarMax;
    calendarMax.setMaximumDate(calendarMax.selectedDate().addDays(7));
    calendarMax.setGridVisible(true);
    calendarMax.show();

    return app.exec();
}
Exemplo n.º 5
0
void tst_QCalendarWidget::buttonClickCheck()
{
    QCalendarWidget object;
    QSize size = object.sizeHint();
    object.setGeometry(0,0,size.width(), size.height());
    object.show();
    object.activateWindow();
    QVERIFY(QTest::qWaitForWindowActive(&object));

    QDate selectedDate(2005, 1, 1);
    //click on the month buttons
    int month = object.monthShown();
    QToolButton *button = object.findChild<QToolButton *>("qt_calendar_prevmonth");
    QTest::mouseClick(button, Qt::LeftButton);
    QCOMPARE(month > 1 ? month-1 : 12, object.monthShown());
    button = object.findChild<QToolButton *>("qt_calendar_nextmonth");
    QTest::mouseClick(button, Qt::LeftButton);
    QCOMPARE(month, object.monthShown());

    button = object.findChild<QToolButton *>("qt_calendar_yearbutton");
    QTest::mouseClick(button, Qt::LeftButton, Qt::NoModifier, button->rect().center(), 2);
    QVERIFY(!button->isVisible());
    QSpinBox *spinbox = object.findChild<QSpinBox *>("qt_calendar_yearedit");
    QTest::qWait(500);
    QTest::keyClick(spinbox, '2');
    QTest::keyClick(spinbox, '0');
    QTest::keyClick(spinbox, '0');
    QTest::keyClick(spinbox, '6');
    QTest::qWait(500);
    QWidget *widget = object.findChild<QWidget *>("qt_calendar_calendarview");
    QTest::mouseMove(widget);
    QTest::mouseClick(widget, Qt::LeftButton);
    QCOMPARE(2006, object.yearShown());
    object.setSelectedDate(selectedDate);
    object.showSelectedDate();
    QTest::keyClick(widget, Qt::Key_Down);
    QVERIFY(selectedDate != object.selectedDate());

    object.setDateRange(QDate(2006,1,1), QDate(2006,2,28));
    object.setSelectedDate(QDate(2006,1,1));
    object.showSelectedDate();
    button = object.findChild<QToolButton *>("qt_calendar_prevmonth");
    QTest::mouseClick(button, Qt::LeftButton);
    QCOMPARE(1, object.monthShown());

    button = object.findChild<QToolButton *>("qt_calendar_nextmonth");
    QTest::mouseClick(button, Qt::LeftButton);
    QCOMPARE(2, object.monthShown());
    QTest::mouseClick(button, Qt::LeftButton);
    QCOMPARE(2, object.monthShown());

}
Exemplo n.º 6
0
void tst_QCalendarWidget::showPrevNext()
{
    QFETCH(ShowFunc, function);
    QFETCH(QDate, dateOrigin);
    QFETCH(QDate, expectedDate);

    QCalendarWidget calWidget;
    calWidget.show();
    QVERIFY(QTest::qWaitForWindowExposed(&calWidget));
    if(!dateOrigin.isNull()) {
        calWidget.setSelectedDate(dateOrigin);
        calWidget.setCurrentPage(dateOrigin.year(), dateOrigin.month());

        QCOMPARE(calWidget.yearShown(), dateOrigin.year());
        QCOMPARE(calWidget.monthShown(), dateOrigin.month());
    } else {
        QCOMPARE(calWidget.yearShown(), QDate::currentDate().year());
        QCOMPARE(calWidget.monthShown(), QDate::currentDate().month());
    }

    (calWidget.*function)();

    QCOMPARE(calWidget.yearShown(), expectedDate.year());
    QCOMPARE(calWidget.monthShown(), expectedDate.month());

    // QTBUG-4058
    QTest::qWait(20);
    QToolButton *button = calWidget.findChild<QToolButton *>("qt_calendar_prevmonth");
    QTest::mouseClick(button, Qt::LeftButton);
    expectedDate = expectedDate.addMonths(-1);
    QCOMPARE(calWidget.yearShown(), expectedDate.year());
    QCOMPARE(calWidget.monthShown(), expectedDate.month());

    if(!dateOrigin.isNull()) {
        //the selectedDate should not have changed
        QCOMPARE(calWidget.selectedDate(), dateOrigin);
    }
}
Exemplo n.º 7
0
bool QgsAttributeEditor::retrieveValue( QWidget *widget, QgsVectorLayer *vl, int idx, QVariant &value )
{
  if ( !widget )
    return false;

  const QgsField &theField = vl->pendingFields()[idx];
  QgsVectorLayer::EditType editType = vl->editType( idx );
  bool modified = false;
  QString text;

  QSettings settings;
  QString nullValue = settings.value( "qgis/nullValue", "NULL" ).toString();

  QLineEdit *le = qobject_cast<QLineEdit *>( widget );
  if ( le )
  {
    text = le->text();
    modified = le->isModified();
    if ( text == nullValue )
    {
      text = QString::null;
    }
  }

  QTextEdit *te = qobject_cast<QTextEdit *>( widget );
  if ( te )
  {
    text = te->toHtml();
    modified = te->document()->isModified();
    if ( text == nullValue )
    {
      text = QString::null;
    }
  }

  QPlainTextEdit *pte = qobject_cast<QPlainTextEdit *>( widget );
  if ( pte )
  {
    text = pte->toPlainText();
    modified = pte->document()->isModified();
    if ( text == nullValue )
    {
      text = QString::null;
    }
  }

  QComboBox *cb = qobject_cast<QComboBox *>( widget );
  if ( cb )
  {
    if ( editType == QgsVectorLayer::UniqueValues ||
         editType == QgsVectorLayer::ValueMap ||
         editType == QgsVectorLayer::Classification ||
         editType == QgsVectorLayer::ValueRelation )
    {
      text = cb->itemData( cb->currentIndex() ).toString();
      if ( text == nullValue )
      {
        text = QString::null;
      }
    }
    else
    {
      text = cb->currentText();
    }
    modified = true;
  }

  QListWidget *lw = qobject_cast<QListWidget *>( widget );
  if ( lw )
  {
    if ( editType == QgsVectorLayer::ValueRelation )
    {
      text = '{';
      for ( int i = 0, n = 0; i < lw->count(); i++ )
      {
        if ( lw->item( i )->checkState() == Qt::Checked )
        {
          if ( n > 0 )
          {
            text.append( ',' );
          }
          text.append( lw->item( i )->data( Qt::UserRole ).toString() );
          n++;
        }
      }
      text.append( '}' );
    }
    else
    {
      text = QString::null;
    }
    modified = true;
  }

  QSpinBox *sb = qobject_cast<QSpinBox *>( widget );
  if ( sb )
  {
    text = QString::number( sb->value() );
  }

  QAbstractSlider *slider = qobject_cast<QAbstractSlider *>( widget );
  if ( slider )
  {
    text = QString::number( slider->value() );
  }

  QDoubleSpinBox *dsb = qobject_cast<QDoubleSpinBox *>( widget );
  if ( dsb )
  {
    text = QString::number( dsb->value() );
  }

  QCheckBox *ckb = qobject_cast<QCheckBox *>( widget );
  if ( ckb )
  {
    QPair<QString, QString> states = vl->checkedState( idx );
    text = ckb->isChecked() ? states.first : states.second;
  }

  QCalendarWidget *cw = qobject_cast<QCalendarWidget *>( widget );
  if ( cw )
  {
    text = cw->selectedDate().toString();
  }

  le = widget->findChild<QLineEdit *>();
  if ( le )
  {
    text = le->text();
  }

  switch ( theField.type() )
  {
    case QVariant::Int:
    {
      bool ok;
      int myIntValue = text.toInt( &ok );
      if ( ok && !text.isEmpty() )
      {
        value = QVariant( myIntValue );
        modified = true;
      }
      else if ( modified )
      {
        value = QVariant();
      }
    }
    break;
    case QVariant::LongLong:
    {
      bool ok;
      qlonglong myLongValue = text.toLong( &ok );
      if ( ok && !text.isEmpty() )
      {
        value = QVariant( myLongValue );
        modified = true;
      }
      else if ( modified )
      {
        value = QVariant();
      }
    }
    case QVariant::Double:
    {
      bool ok;
      double myDblValue = text.toDouble( &ok );
      if ( ok && !text.isEmpty() )
      {
        value = QVariant( myDblValue );
        modified = true;
      }
      else if ( modified )
      {
        value = QVariant();
      }
    }
    break;
    case QVariant::Date:
    {
      QDate myDateValue = QDate::fromString( text, Qt::ISODate );
      if ( myDateValue.isValid() && !text.isEmpty() )
      {
        value = myDateValue;
        modified = true;
      }
      else if ( modified )
      {
        value = QVariant();
      }
    }
    break;
    default: //string
      modified = true;
      value = QVariant( text );
      break;
  }

  return modified;
}