Skip to content

Commit

Permalink
fixes #151 - Added a Settings checkbox option where the user can sele…
Browse files Browse the repository at this point in the history
…ct if any zero-weight edges will be saved (applies only for saving in GraphML format). This is by default FALSE. Note: To do this, we changed saveToGraphMLFormat() accordingly, using a new edgeExistsVirtual() function.
  • Loading branch information
oxy86 committed Mar 31, 2024
1 parent 1d3ee59 commit 7aa41da
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 30 deletions.
31 changes: 31 additions & 0 deletions src/forms/dialogsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,19 @@ DialogSettings::DialogSettings(QMap<QString, QString> &appSettings,
);






/**
* Options (saving, etc)
**/
ui->saveZeroWeightEdgesChkBox->setChecked(
(appSettings["saveZeroWeightEdges"] == "true") ? true:false
);



/**
* GraphicsWidget (canvas) options
*/
Expand Down Expand Up @@ -355,6 +368,9 @@ DialogSettings::DialogSettings(QMap<QString, QString> &appSettings,
ui->edgeLabelsChkBox->setChecked(
(m_appSettings["initEdgeLabelsVisibility"] == "true") ? true: false
);



/**
* dialog signals to slots
*/
Expand Down Expand Up @@ -499,6 +515,11 @@ DialogSettings::DialogSettings(QMap<QString, QString> &appSettings,
connect (ui->edgeLabelsChkBox, &QCheckBox::stateChanged,
this, &DialogSettings::getEdgeLabelsVisibility);


connect (ui->saveZeroWeightEdgesChkBox, &QCheckBox::stateChanged,
this, &DialogSettings::getSaveZeroWeightEdges);


connect ( ui->buttonBox, &QDialogButtonBox::accepted,
this, &DialogSettings::validateSettings );

Expand Down Expand Up @@ -992,6 +1013,16 @@ void DialogSettings::getEdgeLabelsVisibility(const bool &toggle){
}


/**
* @brief Gets the value of saveZeroWeightEdgesChkBox
* @param toggle
*/
void DialogSettings::getSaveZeroWeightEdges(const bool &toggle){
m_appSettings["saveZeroWeightEdges"]= (toggle) ? "true" : "false";
emit setSaveZeroWeightEdges(toggle);
}



DialogSettings::~DialogSettings()
{
Expand Down
3 changes: 3 additions & 0 deletions src/forms/dialogsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public slots:
void getEdgeWeightNumbersVisibility(const bool &toggle);
void getEdgeLabelsVisibility(const bool &toggle);

void getSaveZeroWeightEdges(const bool &toggle);

signals:
void setReportsDataDir (const QString &dir);

Expand Down Expand Up @@ -132,6 +134,7 @@ public slots:
void setEdgeOffsetFromNode(const int&offset, const int &v1=0, const int &v2=0);
void setEdgeWeightNumbersVisibility(const bool &toggle);
void setEdgeLabelsVisibility(const bool &toggle);
void setSaveZeroWeightEdges(const bool &toggle);
void saveSettings();
private:
QMap<QString, QString> &m_appSettings ;
Expand Down
55 changes: 41 additions & 14 deletions src/forms/dialogsettings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@
<string>Settings &amp; Preferences</string>
</property>
<layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0">
<item row="2" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QTabWidget" name="tabWidget">
<property name="minimumSize">
<size>
Expand Down Expand Up @@ -2256,19 +2269,33 @@ p, li { white-space: pre-wrap; }
</item>
</layout>
</widget>
</widget>
</item>
<item row="1" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
<property name="centerButtons">
<bool>false</bool>
</property>
<widget class="QWidget" name="optionsTab">
<attribute name="title">
<string>Options</string>
</attribute>
<widget class="QGroupBox" name="groupBox">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>321</width>
<height>67</height>
</rect>
</property>
<property name="title">
<string>Saving options</string>
</property>
<layout class="QGridLayout" name="gridLayout_8">
<item row="0" column="0">
<widget class="QCheckBox" name="saveZeroWeightEdgesChkBox">
<property name="text">
<string>Save zero-weight edges (GraphML only)</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</widget>
</item>
</layout>
Expand Down
69 changes: 59 additions & 10 deletions src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2567,7 +2567,38 @@ qreal Graph::edgeExists (const int &v1, const int &v2, const bool &checkReciproc
}


/**
* @brief Checks if there is an edge from v1 to v2, even weight = 0 and returns the weight, if the edge exists
* or RAND_MAX if the edge does not exist at all.
*
* This is only used in GraphML saving if the user has selected the Settings option to save zero-weight edges
*
* @see https://github.com/socnetv/app/issues/151
*
* @param v1
* @param v2
*/
qreal Graph::edgeExistsVirtual (const int &v1, const int &v2) {

qreal m_weight=RAND_MAX;
bool edgeStatus=false;
H_edges::const_iterator it1;
GraphVertex *source = m_graph[ vpos[v1] ];
H_edges source_outEdges = source ->m_outEdges;

it1= source_outEdges.constFind(v2);
while (it1 != source_outEdges.constEnd() && it1.key() == v2 ) {
if ( it1.value().first == m_curRelation ) {
edgeStatus=it1.value().second.second;
if ( edgeStatus == true) {
m_weight=it1.value().second.first;
}
}
++it1;
}

return m_weight;
}

/**
* @brief Returns TRUE if edge(v1, v2) is symmetric, i.e. (v1,v2) == (v2,v1).
Expand Down Expand Up @@ -16046,7 +16077,8 @@ void Graph::graphFileLoaded (const int &fileType,
*/
void Graph::saveToFile(const QString &fileName,
const int &fileType ,
const bool &saveEdgeWeights)
const bool &saveEdgeWeights,
const bool &saveZeroWeightEdges)
{
qDebug() << "Saving current graph to file named:" << fileName;
bool saved = false;
Expand All @@ -16064,7 +16096,7 @@ void Graph::saveToFile(const QString &fileName,
break;
}
case FileType::GRAPHML: {
saved=saveToGraphMLFormat(fileName);
saved=saveToGraphMLFormat(fileName, saveZeroWeightEdges);
break;
}
default: {
Expand Down Expand Up @@ -16252,7 +16284,8 @@ bool Graph::saveToDotFormat (QString fileName){
* @return bool
*/
bool Graph::saveToGraphMLFormat (const QString &fileName,
QString networkName,
const bool &saveZeroWeightEdges,
QString networkName,
int maxWidth,
int maxHeight) {

Expand Down Expand Up @@ -16488,13 +16521,21 @@ bool Graph::saveToGraphMLFormat (const QString &fileName,
if (isDirected()) {
for (it=m_graph.cbegin(); it!=m_graph.cend(); ++it)
{
for (jt=m_graph.begin(); jt!=m_graph.end(); jt++)
for (jt=m_graph.cbegin(); jt!=m_graph.cend(); jt++)
{
source=(*it)->number();
target=(*jt)->number();
m_label = "";
weight= edgeExists( source,target ) ;
if ( weight !=0 )

// Check if user opted to save zero-weight edges
if (saveZeroWeightEdges) {
weight= this->edgeExistsVirtual( source,target ) ;
}
else {
weight= this->edgeExists( source,target ) ;
}

if ( ( !saveZeroWeightEdges && weight != 0 ) || (saveZeroWeightEdges && weight != RAND_MAX) )
{
++edgeCount;
m_color = (*it)->outLinkColor( target );
Expand All @@ -16510,7 +16551,7 @@ bool Graph::saveToGraphMLFormat (const QString &fileName,
<< "\" target=\"" << target << "\"";

openToken = true;
if ( weight != 0 ) {
if ( weight != 0 || (saveZeroWeightEdges && weight != RAND_MAX) ) {
outText << "> \n";
outText << " <data key=\"d8\">" << weight<<"</data>" <<" \n";
openToken=false;
Expand Down Expand Up @@ -16545,9 +16586,17 @@ bool Graph::saveToGraphMLFormat (const QString &fileName,
{
source=(*it)->number();
target=(*jt)->number();
weight= edgeExists( source,target );
m_label = "";
if ( weight !=0 )

// Check if user opted to save zero-weight edges
if (saveZeroWeightEdges) {
weight= this->edgeExistsVirtual( source,target ) ;
}
else {
weight= this->edgeExists( source,target ) ;
}

if ( ( !saveZeroWeightEdges && weight != 0 ) || (saveZeroWeightEdges && weight != RAND_MAX) )
{
++edgeCount;
m_color = (*it)->outLinkColor( target );
Expand All @@ -16563,7 +16612,7 @@ bool Graph::saveToGraphMLFormat (const QString &fileName,
<< "\" target=\"" << target << "\"";

openToken = true;
if ( weight !=0 ) {
if ( weight !=0 || (saveZeroWeightEdges && weight != RAND_MAX) ) {
outText << "> \n";
outText << " <data key=\"d8\">" << weight<<"</data>" <<" \n";
openToken=false;
Expand Down
6 changes: 5 additions & 1 deletion src/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,8 @@ public slots:

void saveToFile(const QString &fileName,
const int &fileType,
const bool &saveEdgeWeights=true);
const bool &saveEdgeWeights=true,
const bool &saveZeroWeightEdges=false);

bool saveToPajekFormat (const QString &fileName,
QString networkName="",
Expand All @@ -456,6 +457,7 @@ public slots:
const bool &saveEdgeWeights=true);

bool saveToGraphMLFormat (const QString &fileName,
const bool &saveZeroWeightEdges=false,
QString networkName="",
int maxWidth=0, int maxHeight=0);

Expand Down Expand Up @@ -613,6 +615,8 @@ public slots:
const int &v2,
const bool &checkReciprocal=false);

qreal edgeExistsVirtual(const int &v1, const int &v2);

void edgeOutboundStatusSet (const int &source,
const int &target,
const bool &toggle=false);
Expand Down
37 changes: 32 additions & 5 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,8 @@ QMap<QString,QString> MainWindow::initSettings(const int &debugLevel, const bool
appSettings["initReportsLabelsLength"] = "16";
appSettings["initReportsChartType"] = "0";

appSettings["saveZeroWeightEdges"] = "false";

// Try to load settings from previously-saved file
// First check if our settings folder exist
QDir socnetvDir(settingsDir);
Expand Down Expand Up @@ -843,6 +845,8 @@ void MainWindow::slotOpenSettingsDialog() {
connect( m_settingsDialog, &DialogSettings::setEdgeLabelsVisibility,
this, &MainWindow::slotOptionsEdgeLabelsVisibility);

connect( m_settingsDialog, &DialogSettings::setSaveZeroWeightEdges,
this, &MainWindow::slotOptionsSaveZeroWeightEdges);

// show settings dialog
m_settingsDialog->exec();
Expand Down Expand Up @@ -6849,24 +6853,28 @@ void MainWindow::slotNetworkSave(const int &fileFormat) {

fileNameNoPath = fileInfo.fileName();

bool saveZeroWeightEdges = appSettings["saveZeroWeightEdges"] == "true" ? true:false;

bool saveEdgeWeights = true;

// if the specified format is one of the supported ones, just save it.
if ( activeGraph->isFileFormatExportSupported( fileFormat ) )
{
activeGraph->saveToFile(fileName, fileFormat ) ;
activeGraph->saveToFile(fileName, fileFormat, saveEdgeWeights, saveZeroWeightEdges );
}
// else if it is GraphML or new file not saved yet, just save it.
else if (activeGraph->getFileFormat()==FileType::GRAPHML ||
( activeGraph->isSaved() && !activeGraph->isLoaded() )
)
{
activeGraph->saveToFile(fileName, FileType::GRAPHML);
activeGraph->saveToFile(fileName, FileType::GRAPHML, saveEdgeWeights, saveZeroWeightEdges);
}
// else check whether Graph thinks this is supported and save it
else if ( activeGraph->isFileFormatExportSupported(
activeGraph->getFileFormat()
) )
{
activeGraph->saveToFile(fileName, activeGraph->getFileFormat() ) ;
activeGraph->saveToFile(fileName, activeGraph->getFileFormat(), saveEdgeWeights, saveZeroWeightEdges );
}
// In any other case, save in GraphML.
// First, inform the user that we will save in that format.
Expand All @@ -6889,7 +6897,7 @@ void MainWindow::slotNetworkSave(const int &fileFormat) {
fileName.append(".graphml");
fileNameNoPath = QFileInfo (fileName).fileName();
setLastPath(fileName); // store this path
activeGraph->saveToFile(fileName, FileType::GRAPHML);
activeGraph->saveToFile(fileName, FileType::GRAPHML, saveEdgeWeights, saveZeroWeightEdges);
break;
case QMessageBox::Cancel:
case QMessageBox::No:
Expand Down Expand Up @@ -8092,7 +8100,7 @@ void MainWindow::slotNetworkExportSM(){

}

activeGraph->saveToFile(fileName, FileType::ADJACENCY, saveEdgeWeights ) ;
activeGraph->saveToFile(fileName, FileType::ADJACENCY, saveEdgeWeights ) ;

}

Expand Down Expand Up @@ -14129,7 +14137,26 @@ void MainWindow::slotOptionsEdgeLabelsVisibility(bool toggle) {
}


/**
* @brief Turns on/off saving zero-edge edge weights (only for GraphML at the moment)
* @param toggle
*/
void MainWindow::slotOptionsSaveZeroWeightEdges(bool toggle) {
qDebug() << "MW::slotOptionsSaveZeroWeightEdges - Toggling saving zero weight edges";
QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
statusMessage( tr("Toggle zero-weight edges saving. Please wait...") );

appSettings["saveZeroWeightEdges"] = (toggle) ? "true":"false";

if (toggle) {
statusMessage( tr("Zero-weight edges will be saved to graphml files. ") );
}
else{
statusMessage( tr("Zero-weight edges will NOT be saved to graphml files.") );
}
QApplication::restoreOverrideCursor();

}


/**
Expand Down
Loading

0 comments on commit 7aa41da

Please sign in to comment.