1 #include <QApplication>
17 int main(
int argc,
char* argv[] )
19 QApplication application( argc, argv );
26 return application.exec();
32 connect(
this, SIGNAL( clicked() ), SLOT(
pushed() ) );
54 qDebug() <<
"CG: nefmods" << nefmods;
56 setWindowTitle( tr(
"Color Gradient Generator" ) );
60 QBoxLayout* topbox =
new QVBoxLayout(
this );
61 topbox->setSpacing( 2 );
63 QGridLayout* gbuttons =
new QGridLayout();
65 QString btext = tr(
"Please select the number of steps\n"
66 "and each Color Step for your gradient:\n"
67 "( Currently 1 step; 2 total colors;\n" );
75 connect(
ct_nsteps, SIGNAL( valueChanged(
double ) ),
78 QHBoxLayout* steps =
new QHBoxLayout();
83 topbox->addLayout( steps );
85 QGridLayout* colors =
new QGridLayout();
88 QStyle* btnsty =
new QPlastiqueStyle();
89 QString stynam = qApp->style()->objectName();
90 bool needbsty = stynam.startsWith(
"windowsv", Qt::CaseInsensitive ) ||
91 stynam.startsWith(
"windowsx", Qt::CaseInsensitive ) ||
92 stynam.startsWith(
"mac" , Qt::CaseInsensitive );
95 for (
int ii = 0; ii < 11; ii++ )
100 pb_c[ ii ]->setStyle( btnsty );
103 ct_c[ ii ]->setStep( 1.0 );
105 colors->addWidget(
pb_c[ ii ], c_row, 0 );
106 colors->addWidget(
ct_c[ ii ], c_row++, 1 );
108 connect(
pb_c[ ii ], SIGNAL( click (
int ) ),
111 connect(
ct_c[ ii ], SIGNAL( valueChanged (
double ) ),
116 pb_c[ ii ]->setVisible(
false );
117 ct_c[ ii ]->setVisible(
false );
121 ct_c[ ii ]->setVisible(
false );
127 topbox->addLayout( colors );
133 connect(
pb_help, SIGNAL( clicked() ),
this, SLOT(
help() ) );
134 connect(
pb_reset, SIGNAL( clicked() ),
this, SLOT(
reset() ) );
135 gbuttons->addWidget(
pb_help, row, 0 );
136 gbuttons->addWidget(
pb_reset, row++, 1 );
142 gbuttons->addWidget(
pb_load, row, 0 );
143 gbuttons->addWidget(
pb_show, row++, 1 );
149 gbuttons->addWidget(
pb_save, row, 0 );
150 gbuttons->addWidget(
pb_close, row++, 1 );
153 topbox->addLayout( gbuttons );
156 QBoxLayout* showgrad =
new QHBoxLayout();
164 topbox->addLayout( showgrad );
173 QPalette
p =
pb_c[ which ]->palette();
175 QColor color = QColorDialog::getColor( p.color( QPalette::Button ), this );
177 if ( color.isValid() )
179 p.setColor( QPalette::Button, color );
180 pb_c[ which ]->setPalette( p );
202 ct_c[ 1 ]->setValue( 1.0 );
214 QString save_file =
grad_dir +
"/new_gradient.xml";
215 save_file = QFileDialog::getSaveFileName(
this,
216 tr(
"Specify XML File Name for Gradient Save" ),
grad_dir,
217 tr(
"Color Map files (*cm-*.xml);;"
218 "Any XML files (*.xml);;Any files (*)" ) );
220 save_file = save_file.replace(
"\\",
"/" );
221 int jj = save_file.lastIndexOf(
"/" ) + 1;
222 QString fdir = save_file.left( jj );
223 QString fnam = save_file.mid( jj );
225 if ( !save_file.isEmpty() )
230 if ( save_file.endsWith(
"." ) )
232 save_file = save_file.left( save_file.length() - 1 );
233 fnam = fnam .left( fnam .length() - 1 );
236 else if ( ! save_file.endsWith(
".xml" ) )
238 save_file = save_file +
".xml";
239 fnam = fnam +
".xml";
242 if ( fnam.startsWith(
"." ) )
244 save_file = fdir + fnam.mid( 1 );
247 else if ( ! fnam.startsWith(
"cm-" ) )
249 save_file = fdir +
"cm-" + fnam;
260 if ( !fileo.open( QIODevice::WriteOnly | QIODevice::Text ) )
262 QMessageBox::information(
this,
268 QXmlStreamWriter xmlo;
269 xmlo.setDevice( &fileo );
270 xmlo.setAutoFormatting(
true );
271 xmlo.writeStartDocument(
"1.0" );
272 xmlo.writeComment(
"DOCTYPE UltraScanColorSteps" );
273 xmlo.writeCharacters(
"\n" );
274 xmlo.writeStartElement(
"colorsteps" );
278 QColor s_color =
pb_c[ 0 ]->palette().color( QPalette::Button );
279 xmlo.writeStartElement(
"color" );
280 xmlo.writeAttribute(
"red", QString::number( s_color.red() ) );
281 xmlo.writeAttribute(
"green", QString::number( s_color.green() ) );
282 xmlo.writeAttribute(
"blue", QString::number( s_color.blue() ) );
283 xmlo.writeAttribute(
"points",
"0" );
284 xmlo.writeEndElement();
285 int nsteps = qRound(
ct_nsteps->value() );
289 for (
int ii = 1; ii <= nsteps; ii++ )
291 s_color =
pb_c[ ii ]->palette().color( QPalette::Button );
292 npoints = qRound(
ct_c[ ii ]->value() );
293 xmlo.writeStartElement(
"color" );
294 xmlo.writeAttribute(
"red", QString::number( s_color.red() ) );
295 xmlo.writeAttribute(
"green", QString::number( s_color.green() ) );
296 xmlo.writeAttribute(
"blue", QString::number( s_color.blue() ) );
297 xmlo.writeAttribute(
"points", QString::number( npoints ) );
298 xmlo.writeEndElement();
301 xmlo.writeEndElement();
302 xmlo.writeEndDocument();
307 .replace( QRegExp(
".xml$" ), QString(
".png" ) );
309 int widp = mcolors->width();
310 int hgtp = mcolors->height();
311 int recx = (int)( (qreal)widp * ( 1.0 -
lgndfrac ) );
312 int widl = widp - recx - 2;
314 QString msg = tr(
"Do you want to also save the %1 x %2 color legend"
315 " image\n in file \"%3\" .\n" )
316 .arg(widl).arg(hgtl).arg(png_fname);
318 msgBox.setText( msg );
319 msgBox.setStandardButtons( QMessageBox::No | QMessageBox::Yes );
320 msgBox.setDefaultButton( QMessageBox::Yes );
321 int result = msgBox.exec();
322 if ( result == QMessageBox::Yes )
324 QPixmap clegend = mcolors->copy( recx, 0, widl, hgtl );
325 clegend.toImage().save( png_fname );
337 msgBox.setTextFormat( Qt::RichText );
338 msgBox.setText( tr(
"You have modified the gradient since the "
339 "last save to file.<br>"
340 "Do you want to save to file before exiting?<ul>"
341 "<li><b>[Yes]</b> to save and close;</li>"
342 "<li><b>[No]</b> to close with no save;</li>"
343 "<li><b>[Cancel]</b> to resume gradient modifications.</li></ul>" ) );
344 msgBox.addButton( QMessageBox::Cancel );
345 msgBox.addButton( QMessageBox::No );
346 msgBox.addButton( QMessageBox::Yes );
347 msgBox.setDefaultButton( QMessageBox::Yes );
348 int result = msgBox.exec();
350 if ( result == QMessageBox::Yes )
354 else if ( result == QMessageBox::Cancel )
367 QString load_file =
grad_dir +
"/old_gradient.xml";
368 load_file = QFileDialog::getOpenFileName(
this,
369 tr(
"Select XML File Name for Gradient Load" ),
grad_dir,
370 tr(
"Color Map files (*cm-*.xml);;"
371 "Any XML files (*.xml);;Any files (*)" ) );
373 if ( !load_file.isEmpty() )
377 if ( !load_file.endsWith(
"." ) && !load_file.endsWith(
".xml" ) )
384 if ( !filei.open( QIODevice::ReadOnly ) )
386 QMessageBox::information(
this,
392 QXmlStreamReader xmli( &filei );
393 bool is_uscs =
false;
399 while ( ! xmli.atEnd() )
403 if ( xmli.isComment() )
405 QString comm = xmli.text().toString();
407 if ( comm.contains(
"UltraScanColorSteps" ) )
413 QMessageBox::information(
this, tr(
"Error" ),
415 + tr(
" is not an UltraScanColorSteps xml file.") );
421 if ( xmli.isStartElement() && xmli.name() ==
"color" )
423 QXmlStreamAttributes ats = xmli.attributes();
424 int cred = ats.value(
"red" ).toString().toInt();
425 int cgrn = ats.value(
"green" ).toString().toInt();
426 int cblu = ats.value(
"blue" ).toString().toInt();
427 QColor s_color = QColor( cred, cgrn, cblu );
428 int npoints = ats.value(
"points" ).toString().toInt();
441 QPalette pa =
pb_c[ nsteps ]->palette();
442 pa.setColor( QPalette::Button, s_color );
443 pb_c[ nsteps ]->setPalette( pa );
444 ct_c[ nsteps ]->setValue( (
double)npoints );
448 if ( xmli.hasError() )
450 QMessageBox::information(
this, tr(
"Error" ),
451 tr(
"File " ) +
in_filename + tr(
" is not a valid XML file.") );
453 else if ( ! is_uscs )
455 QMessageBox::information(
this, tr(
"Error" ),
457 + tr(
" is not an UltraScanColorSteps xml file.") );
479 int nsteps = qRound( newval );
482 for (
int ii = 2; ii < 11; ii++ )
484 bool show = ( ii <= nsteps ) ?
true :
false;
485 pb_c[ ii ]->setVisible( show );
486 ct_c[ ii ]->setVisible( show );
499 int widthp = widthl -
margin;
500 qreal wlbl = (qreal)widthl;
502 qreal hlbl = wlbl - wrec - 8.0;
503 int heightl = (int)hlbl;
504 int heightp = heightl -
margin;
505 int nsteps = qRound(
ct_nsteps->value() );
512 heightp = heightl -
margin;
525 resize( widthl, heightl );
528 ct_c[ 1 ]->setValue( 1.0 );
538 for (
int ii = 1; ii <= nsteps; ii++ )
540 int npoints = qRound(
ct_c[ ii ]->value() );
545 qreal radi = (qreal)heightp / 2.0;
546 qreal thikc = radi / (qreal)ncolors;
547 qreal x1leg = heightp + thikc + 4.0;
548 qreal x2leg = (qreal)widthp - 4.0;
549 qreal y1leg = (qreal)heightp;
550 qreal dyleg = y1leg / (qreal)ncolors;
551 qreal thikl = dyleg + 1.0;
552 y1leg -= ( dyleg / 2.0 );
553 radi -= ( thikc / 2.0 );
554 QPointF cenpt( radi, radi );
555 QColor cc =
pb_c[ 0 ]->palette().color( QPalette::Button );
559 for (
int ii = 1; ii <= nsteps; ii++ )
562 int npoints = qRound(
ct_c[ ii ]->value() );
563 QColor ec =
pb_c[ ii ]->palette().color( QPalette::Button );
566 qreal rngp = (qreal)npoints;
567 qreal delr = (qreal)( ec.red() - cc.red() ) / rngp;
568 qreal delg = (qreal)( ec.green() - cc.green() ) / rngp;
569 qreal delb = (qreal)( ec.blue() - cc.blue() ) / rngp;
572 qreal dvlr = (qreal)cc.red();
573 qreal dvlg = (qreal)cc.green();
574 qreal dvlb = (qreal)cc.blue();
578 for (
int jj = 0; jj < npoints ; jj++ )
581 int ivlr = qRound( dvlr );
582 int ivlg = qRound( dvlg );
583 int ivlb = qRound( dvlb );
584 QBrush brc = QBrush( QColor( ivlr, ivlg, ivlb ) );
587 pa->setPen( QPen( brc, thikc+2.0 ) );
588 pa->drawEllipse( cenpt, radi, radi );
591 pa->setPen( QPen( brc, thikl ) );
592 pa->drawLine( QPointF( x1leg, y1leg ), QPointF( x2leg, y1leg ) );
606 QBrush brc = QBrush( cc );
607 pa->setPen( QPen( brc, thikc ) );
608 pa->drawEllipse( cenpt, radi, radi );
610 pa->setPen( QPen( brc, thikl ) );
611 pa->drawLine( QPointF( x1leg, y1leg ), QPointF( x2leg, y1leg ) );
627 int nsteps = qRound(
ct_nsteps->value() );
629 for (
int ii = 1; ii <= nsteps; ii++ )
631 ncolors += qRound(
ct_c[ ii ]->value() );
634 QString steps = ( nsteps == 1 ) ?
636 QString::number( nsteps ) +
" steps";
637 QString colors = QString::number( ncolors );
639 QString btext = tr(
"Please select the number of steps\n"
640 "and each Color Step for your gradient:\n"
641 "( Currently %1; %2 total colors;\n" )
642 .arg(steps).arg(colors);
646 btext += tr(
" Save file: " )
651 btext += tr(
" Not saved to any file )" );