Skip to content

Commit

Permalink
Merge pull request #32 from DUNE-DAQ/gcrone/readonly-schema
Browse files Browse the repository at this point in the history
Don't try to set file active if it's read-only
  • Loading branch information
gcrone authored Oct 2, 2024
2 parents b5128af + 03bbce2 commit 6b250d1
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 78 deletions.
25 changes: 22 additions & 3 deletions apps/SchemaEditor/SchemaCustomFileModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
/// Including C++ Headers
#include <vector>

#include <QSize>

dbse::CustomFileModel::CustomFileModel ( QStringList & Headers, QObject * parent )
: QAbstractTableModel ( parent ),
HeaderList ( Headers )
Expand All @@ -29,8 +31,12 @@ int dbse::CustomFileModel::columnCount ( const QModelIndex & parent ) const

Qt::ItemFlags dbse::CustomFileModel::flags ( const QModelIndex & index ) const
{
Q_UNUSED ( index )
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if ( Data.value (index.row() ).value ( 1) == "RW" ) {
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
else {
return Qt::NoItemFlags;
}
}

QVariant dbse::CustomFileModel::headerData ( int section, Qt::Orientation orientation,
Expand Down Expand Up @@ -63,7 +69,8 @@ void dbse::CustomFileModel::setupModel()
{
std::vector<std::string> SchemaFiles;
KernelWrapper::GetInstance().GetSchemaFiles ( SchemaFiles );

std::string ActiveSchema = KernelWrapper::GetInstance().GetActiveSchema();
std::string Modified = KernelWrapper::GetInstance().ModifiedSchemaFiles();
for ( std::string & FileName : SchemaFiles )
{
QStringList Row;
Expand All @@ -78,6 +85,18 @@ void dbse::CustomFileModel::setupModel()
Row.append ( "RO" );
}

std::string Status = "";
if ( Modified.find( FileName ) != std::string::npos) {
Status = "Modified";
}
if ( FileName == ActiveSchema) {
if ( !Status.empty() ) {
Status += " ";
}
Status += "Active";
}

Row.append ( QString::fromStdString ( Status ) );
Data.append ( Row );
}
}
Expand Down
42 changes: 36 additions & 6 deletions apps/SchemaEditor/SchemaKernelWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ void dbse::KernelWrapper::SetActiveSchema ( const std::string & ActiveSchema )
{
OksFile * File = Kernel->find_schema_file ( ActiveSchema );

if ( File )
if ( File && IsFileWritable( ActiveSchema ))
{
Kernel->set_active_schema ( File );
}
else
{
/// This never should happen
}
}

void dbse::KernelWrapper::GetClassList ( std::vector<OksClass *> & ClassList ) const
Expand Down Expand Up @@ -59,6 +55,17 @@ void dbse::KernelWrapper::GetClassListString ( QStringList & ClassListString ) c
}
}

std::string dbse::KernelWrapper::GetActiveSchema () const
{
auto file = Kernel->get_active_schema();
if ( file) {
return file->get_full_file_name();
}
else {
return "";
}
}

void dbse::KernelWrapper::GetSchemaFiles ( std::vector<std::string> & SchemaFiles )
{
for ( OksFile::Map::const_iterator i = Kernel->schema_files().begin();
Expand All @@ -74,7 +81,7 @@ void dbse::KernelWrapper::GetIncludedList ( const std::string & FileName,
Kernel->get_includes ( FileName, IncludedFiles );
}

bool dbse::KernelWrapper::IsFileWritable ( std::string & FileName ) const
bool dbse::KernelWrapper::IsFileWritable (const std::string & FileName ) const
{
OksFile * File = Kernel->find_schema_file ( FileName );

Expand All @@ -101,6 +108,29 @@ void dbse::KernelWrapper::SaveAllSchema() const
Kernel->save_all_schema();
}

std::string dbse::KernelWrapper::ModifiedSchemaFiles() const
{
std::string modified{""};
for (auto [name, file] : Kernel->schema_files()) {
if (file->is_updated()) {
modified += file->get_full_file_name() + "\n\n";
}
}
return modified;
}

std::string dbse::KernelWrapper::SaveModifiedSchema() const
{
std::string saved{""};
for (auto [name, file] : Kernel->schema_files()) {
if (file->is_updated()) {
Kernel->save_schema(file);
saved += file->get_full_file_name() + "\n\n";
}
}
return saved;
}

void dbse::KernelWrapper::CloseAllSchema() const
{
Kernel->close_all_schema();
Expand Down
65 changes: 42 additions & 23 deletions apps/SchemaEditor/SchemaMainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <QPrinter>
#include <QPrintDialog>

//#include <format>
#include <sstream>

using namespace dunedaq;
using namespace dunedaq::oks;

Expand Down Expand Up @@ -75,13 +78,13 @@ void dbse::SchemaMainWindow::InitialTabCorner()
void dbse::SchemaMainWindow::SetController()
{
connect ( ui->OpenFileSchema, SIGNAL ( triggered() ), this, SLOT ( OpenSchemaFile() ) );
connect ( ui->LoadFileSchema, SIGNAL ( triggered() ), this, SLOT ( OpenSchemaFile() ) );
connect ( ui->CreateNewSchema, SIGNAL ( triggered() ), this, SLOT ( CreateNewSchema() ) );
connect ( ui->SaveSchema, SIGNAL ( triggered() ), this, SLOT ( SaveSchema() ) );
connect ( ui->SetRelationship, SIGNAL ( triggered ( bool ) ), this,
SLOT ( ChangeCursorRelationship ( bool ) ) );
connect ( ui->SetInheritance, SIGNAL ( triggered ( bool ) ), this,
SLOT ( ChangeCursorInheritance ( bool ) ) );
connect ( ui->AddClass, SIGNAL ( triggered() ), this, SLOT ( AddNewClass() ) );
connect ( ui->SaveView, SIGNAL ( triggered() ), this, SLOT ( SaveView() ) );
connect ( ui->LoadView, SIGNAL ( triggered() ), this, SLOT ( LoadView() ) );
connect ( ui->Exit, SIGNAL ( triggered() ), this, SLOT ( close() ) );
Expand All @@ -104,7 +107,7 @@ void dbse::SchemaMainWindow::SetController()
void dbse::SchemaMainWindow::BuildFileModel()
{
QStringList Headers
{ "File Name", "Access" };
{ "File Name", "Access", "Status" };

if ( FileModel == nullptr )
{
Expand All @@ -115,8 +118,10 @@ void dbse::SchemaMainWindow::BuildFileModel()
delete FileModel;
FileModel = new CustomFileModel ( Headers );
}

ui->FileView->setModel ( FileModel );
ui->FileView->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->FileView->horizontalHeader()->setSectionResizeMode(1, QHeaderView::ResizeToContents);
ui->FileView->horizontalHeader()->setSectionResizeMode(2, QHeaderView::ResizeToContents);
}

void dbse::SchemaMainWindow::BuildTableModel()
Expand All @@ -140,20 +145,25 @@ void dbse::SchemaMainWindow::BuildTableModel()

int dbse::SchemaMainWindow::ShouldSaveChanges() const
{
if ( KernelWrapper::GetInstance().GetUndoStack()->isClean() )
// if ( KernelWrapper::GetInstance().GetUndoStack()->isClean() )
auto modified = KernelWrapper::GetInstance().ModifiedSchemaFiles();
if (modified.empty())
{
return QMessageBox::Discard;
}

std::string msg = "There are unsaved changes in the following files:\n\n"
+ modified + "Do you want to save the changes in the schema?\n";
return QMessageBox::question (
0, tr ( "DBE" ),
QString ( "There are unsaved changes.\n\nDo you want to save the changes in the schema?\n" ),
QString ( msg.c_str() ),
QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save );
}

void dbse::SchemaMainWindow::AddNewClass()
{
SchemaClassEditor::createNewClass();
BuildFileModel();
}

void dbse::SchemaMainWindow::RemoveClass()
Expand Down Expand Up @@ -182,6 +192,7 @@ void dbse::SchemaMainWindow::RemoveClass()
KernelWrapper::GetInstance().PushRemoveClassCommand ( SchemaClass, SchemaClass->get_name(),
SchemaClass->get_description(),
SchemaClass->get_is_abstract() );
BuildFileModel();
}
}

Expand All @@ -190,6 +201,7 @@ void dbse::SchemaMainWindow::SetSchemaFileActive()
QModelIndex Index = ui->FileView->currentIndex();
QStringList Row = FileModel->getRowFromIndex ( Index );
KernelWrapper::GetInstance().SetActiveSchema ( Row.at ( 0 ).toStdString() );
BuildFileModel();
}

void dbse::SchemaMainWindow::PrintCurrentView()
Expand All @@ -215,16 +227,7 @@ void dbse::SchemaMainWindow::closeEvent ( QCloseEvent * event )

if ( UserChoice == QMessageBox::Save )
{
try
{
KernelWrapper::GetInstance().SaveAllSchema();
KernelWrapper::GetInstance().CloseAllSchema();
}
catch ( oks::exception & Ex )
{
QMessageBox::warning ( 0, "Schema editor",
QString ( "Could not save schemas.\n\n%1" ).arg ( QString ( Ex.what() ) ) );
}
SaveModifiedSchema();
}
else if ( UserChoice == QMessageBox::Discard )
{
Expand Down Expand Up @@ -287,26 +290,44 @@ void dbse::SchemaMainWindow::OpenSchemaFile()

OpenSchemaFile(SchemaPath);
}

void dbse::SchemaMainWindow::SaveSchema()
void dbse::SchemaMainWindow::SaveModifiedSchema()
{
try
try
{
KernelWrapper::GetInstance().SaveAllSchema();
auto saved = KernelWrapper::GetInstance().SaveModifiedSchema();
//std::format msg("{} schema files successfully saved", nsaved)
std::ostringstream ostream;
ostream << "Schema files:\n" + saved + " successfully saved";
std::string msg = ostream.str();
QMessageBox::information ( 0, "Schema editor",
QString ( "Schema successfully saved" ) );
QString ( msg.c_str() ) );

}
catch ( oks::exception & Ex )
{
QMessageBox::warning ( 0, "Schema editor",
QString ( "Could not save schemas.\n\n%1" ).arg ( QString ( Ex.what() ) ) );
}
}

void dbse::SchemaMainWindow::SaveSchema()
{
auto modified = KernelWrapper::GetInstance().ModifiedSchemaFiles();
if (modified.empty())
{
QMessageBox::information ( 0, "Schema editor",
QString ( "No modified schema files need saving" ) );

}
else {
SaveModifiedSchema();
}
BuildFileModel();
}

void dbse::SchemaMainWindow::CreateNewSchema()
{
QString FileName = QFileDialog::getSaveFileName ( this, tr ( "Save File" ) );
QString FileName = QFileDialog::getSaveFileName ( this, tr ( "New schema File" ) );

if ( FileName.isEmpty() )
{
Expand Down Expand Up @@ -547,8 +568,6 @@ void dbse::SchemaMainWindow::CustomContextMenuFileView ( QPoint Pos )
ContextMenuFileView = new QMenu ( this );

QAction * Add = new QAction ( tr ( "&Set Active Schema" ), this );
Add->setShortcut ( tr ( "Ctrl+S" ) );
Add->setShortcutContext ( Qt::WidgetShortcut );
connect ( Add, SIGNAL ( triggered() ), this, SLOT ( SetSchemaFileActive() ) );

ContextMenuFileView->addAction ( Add );
Expand Down
2 changes: 1 addition & 1 deletion apps/SchemaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ int main(int argc, char *argv[]) {

dbse::SchemaMainWindow w(QString::fromStdString(oksfn));

w.showMaximized();
w.show();

return a.exec();
}
28 changes: 2 additions & 26 deletions icons/SchemaResource.qrc
Original file line number Diff line number Diff line change
@@ -1,41 +1,17 @@
<RCC>
<qresource prefix="/icons">
<file>add_class.xpm</file>
<file>aggregation.xpm</file>
<file>comment.xpm</file>
<file>commit.xpm</file>
<file>data_editor_about.xpm</file>
<file>data_editor_oks_object.xpm</file>
<file>default-obj.xpm</file>
<file>error.xpm</file>
<file>CreateDatabase.png</file>
<file>Exit.png</file>
<file>exit.xpm</file>
<file>fail.xpm</file>
<file>glink.xpm</file>
<file>schema_editor_oks_relationship.xpm</file>
<file>select-class.xpm</file>
<file>select.xpm</file>
<file>stop.xpm</file>
<file>tree.xpm</file>
<file>update.xpm</file>
<file>warning.xpm</file>
<file>xbae.xpm</file>
<file>xmhtml.xpm</file>
<file>hand.xpm</file>
<file>info.xpm</file>
<file>link2.xpm</file>
<file>many.xpm</file>
<file>move.xpm</file>
<file>new_class.xpm</file>
<file>ok.xpm</file>
<file>one.xpm</file>
<file>params.xpm</file>
<file>print.xpm</file>
<file>release.xpm</file>
<file>remove.xpm</file>
<file>rename.xpm</file>
<file>revert.xpm</file>
<file>save_all.xpm</file>
<file>save_as.xpm</file>
<file>save.xpm</file>
<file>schema_editor_about.xpm</file>
<file>schema_editor_oks_attribute.xpm</file>
Expand Down
5 changes: 4 additions & 1 deletion include/dbe/SchemaKernelWrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ class KernelWrapper: public QObject
void GetSchemaFiles ( std::vector<std::string> & SchemaFiles );
void GetIncludedList ( const std::string & FileName,
std::set<std::string> & IncludedFiles );
bool IsFileWritable ( std::string & FileName ) const;
bool IsFileWritable ( const std::string & FileName ) const;
bool IsActive() const;
dunedaq::oks::OksClass * FindClass ( std::string ClassName ) const;
void LoadSchema ( const std::string & SchemaName ) const;
void SaveAllSchema() const;
std::string ModifiedSchemaFiles() const;
std::string SaveModifiedSchema() const;
std::string GetActiveSchema() const;
void CloseAllSchema() const;
void CreateNewSchema ( const std::string & SchemaName ) const;
bool AnyClassReferenceThis ( dunedaq::oks::OksClass * SchemaClass );
Expand Down
1 change: 1 addition & 0 deletions include/dbe/SchemaMainWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ private slots:
void OpenSchemaFile();
void CreateNewSchema();
void SaveSchema();
void SaveModifiedSchema();
void ChangeCursorRelationship ( bool State );
void ChangeCursorInheritance ( bool State );
void AddTab();
Expand Down
Loading

0 comments on commit 6b250d1

Please sign in to comment.