Beispiel #1
0
bool PasteCommand::processTextPlain(Element *element)
{
    const QString text = m_mimeData->text();
    if (text.isEmpty()) {
        return false;
    }

    // Split the text into lines.
    const QStringList list = text.split('\n');

    const int mx = 1; // always one column
    const int my = list.count();

    // Put the lines into an array value.
    Value value(Value::Array);
    for (int i = 0; i < list.count(); ++i) {
        value.setElement(0, i, Value(list[i]));
    }

    // FIXME Determine and tile the destination area.
    Region range(mx, my, 1, list.size());

    // create a command, configure it and execute it
    DataManipulator *command = new DataManipulator(this);
    command->setSheet(m_sheet);
    command->setParsing(false);
    command->setValue(value);
    command->add(element->rect(), m_sheet);
    return true;
}
Beispiel #2
0
void DatabaseDialog::accept()
{
  Sheet * sheet = m_selection->activeSheet();
  int top;
  int left;
  int width  = -1;
  int height = -1;
  if ( m_startingRegion->isChecked() )
  {
    Region region(m_region->text(), sheet->map());
    if (region.isValid() && region.firstSheet() != sheet)
    {
      KMessageBox::error( this, i18n("You cannot specify a table here.") );
      m_region->setFocus();
      m_region->selectAll();
      return;
    }

    if (!region.isValid())
    {
      KMessageBox::error( this, i18n("You have to specify a valid region.") );
      m_region->setFocus();
      m_region->selectAll();
      return;
    }

    top    = region.firstRange().top();
    left   = region.firstRange().left();
    width  = region.firstRange().width();
    height = region.firstRange().height();
  }
  else
  {
    const Region region(m_cell->text(), sheet->map(), sheet);
    if (region.isValid() && region.firstSheet() != sheet)
    {
      KMessageBox::error( this, i18n("You cannot specify a table here.") );
      m_cell->setFocus();
      m_cell->selectAll();
      return;
    }
    //    if ( point.pos.x() < 1 || point.pos.y() < 1 )
    if (!region.isValid())
    {
      KMessageBox::error( this, i18n("You have to specify a valid cell.") );
      m_cell->setFocus();
      m_cell->selectAll();
      return;
    }
    top  = region.firstRange().topLeft().y();
    left = region.firstRange().topLeft().x();
  }

  int i;
  QString queryStr;
  QString tmp = m_sqlQuery->toPlainText();
  for ( i = 0; i < (int) tmp.length(); ++i )
  {
    if ( tmp[i] != '\n' )
      queryStr += tmp[i];
    else
      queryStr += ' ';
  }

  Cell cell;
  QSqlQuery query( m_dbConnection );

  // Check the whole query for SQL that might modify database.
  // If there is an update command, then it must be at the start of the string,
  // or after an open bracket (e.g. nested update) or a space to be valid SQL.
  // An update command must also be followed by a space, or it would be parsed
  // as an identifier.
  // For sanity, also check that there is a SELECT
  QRegExp couldModifyDB( "(^|[( \\s])(UPDATE|DELETE|INSERT|CREATE) ", Qt::CaseInsensitive );
  QRegExp couldQueryDB( "(^|[( \\s])(SELECT) ", Qt::CaseInsensitive );

  if (couldModifyDB.indexIn( queryStr ) != -1 || couldQueryDB.indexIn( queryStr ) == -1 )
  {
    KMessageBox::error( this, i18n("You are not allowed to change data in the database.") );
    m_sqlQuery->setFocus();
    return;
  }

  if ( !query.exec( queryStr ) )
  {
    KMessageBox::error( this, i18n( "Executing query failed." ) );
    m_sqlQuery->setFocus();
    return;
  }

  if ( query.size() == 0 )
  {
    KMessageBox::error( this, i18n( "You did not get any results with this query." ) );
    m_sqlQuery->setFocus();
    return;
  }

  int y = 0;
  int count = m_columns_1->count();
  if ( width != -1 )
  {
    if ( count > width )
      count = width;
  }

  if ( height == -1 )
  {
    height = 0;
    if ( query.first() )
    {
      if ( query.isValid() )
        ++height;
    }
    while ( query.next() )
    {
      if ( query.isValid() )
        ++height;
    }
  }

    QUndoCommand* macroCommand = new QUndoCommand(i18n("Insert Data From Database"));

  if ( query.first() )
  {
    if ( query.isValid() )
    {
      for ( i = 0; i < count; ++i )
      {
        DataManipulator* command = new DataManipulator(macroCommand);
        command->setParsing(true);
        command->setSheet(sheet);
        command->setValue(Value(query.value( i ).toString()));
        command->add(Region(left + i, top + y, sheet));
      }
      ++y;
    }
  }

  if ( y != height )
  {
    while ( query.next() )
    {
      if ( !query.isValid() )
        continue;

      for ( i = 0; i < count; ++i )
      {
        DataManipulator* command = new DataManipulator(macroCommand);
        command->setParsing(true);
        command->setSheet(sheet);
        command->setValue(Value(query.value( i ).toString()));
        command->add(Region(left + i, top + y, sheet));
      }
      ++y;

      if ( y == height )
        break;
    }
  }
    m_selection->canvas()->addCommand(macroCommand);

  m_selection->emitModified();
  KAssistantDialog::accept();
}