02 March 2012

What I Did Today

Today I set out to add to the grid a context menu containing a function to delete the items selected.

This plan was only slightly impeded by Spotify (running under Wine) deciding to hang the computer for no sufficient reason.
There was already a slot in my code for the menu, so I just had to fill it in. Any real programmer could have knocked it off in ten minutes; not me, not me...

void Grid::displayMenu ( QPoint qp )
{
 QList fred = ui.theGrid->selectedItems();
 QMenu *menu = new QMenu(this);
 QString deleteNote = QString::fromAscii("Delete Selected");
 QString dummy = QString::fromAscii("dummy");
 menu->addAction(dummy);
 menu->addAction(deleteNote);
 menu->addAction(dummy);

 if (fred.isEmpty())
    {
     QList melvin = menu->actions();
     foreach( QAction *leonard, melvin )
      if (leonard->text() == deleteNote)
       leonard->setDisabled(true);
    }

 QAction* selectedItem = menu->exec( this->ui.theGrid->mapToGlobal(qp) );
 if (selectedItem) // something was chosen, do stuff
    {
     if (selectedItem->text() == deleteNote)
        {
         ConfirmationDialog* confirm = new ConfirmationDialog(this);
         foreach( QTableWidgetItem *george, fred )
                confirm->ui.listWidget->addItem(george->data(Utilities::filePath).toString());
         if (confirm->exec() == QDialog::Accepted)
            {
             if (confirm->ui.confirmationBox->isChecked())
                {
                 QStringList failure;
                 foreach( QTableWidgetItem *george, fred )
                        {
                         if (george)
                            {
                             QDir foo;
                             bool success = foo.remove (george->data(Utilities::filePath).toString());
                             if (success)
                                {
                                 this->ui.theGrid->takeItem(george->row(),george->column());
                                 delete(george);
                                }
                             else
                                {
                                 failure.append(george->data(Utilities::filePath).toString());
                                }
                            }
                        }
                 if (!failure.isEmpty())
                    {
                     confirm->ui.listWidget->clear();
                     confirm->ui.listWidget->addItems(failure);
                     confirm->ui.confirmationBox->hide();
                     foreach(QAbstractButton* button,confirm->ui.buttonBox->buttons() )
                            if (confirm->ui.buttonBox->buttonRole(button) == QDialogButtonBox::RejectRole)
                               button->hide();
                     confirm->ui.prompt->setText(QString::fromAscii("Some selected could not be deleted."));
                     confirm->exec();
                    }
                }
            }
         delete(confirm);
        }
    }
}
Pretty nasty, huh?
On Linux (under the ext4 file system and presumably previous versions) you can set a folder such that only the owner can delete files in it, but setting a file to be undeleteable by the owner requires setting it undeleteable even by root. Which is silly. I'll have to double-check what Windows has to offer in that area.
The confirmation dialog is new and caused a lot of bother, as adding a simple checkbox to one of Qt Creator's premixed dialogs is trickier than I thought. I also handle it clunkily.

No comments:

Post a Comment