1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
|
/****************************************************************************
* dividedlg.cpp - divide lines, circles and arcs *
* *
* Copyright (C) 2018 mad-hatter *
* *
* This library is free software, licensed under the terms of the GNU *
* General Public License as published by the Free Software Foundation, *
* either version 2 of the License, or (at your option) any later version. *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************/
#include <QtGui>
#include <QApplication>
#include <QDesktopWidget>
#include <QDialog>
#include <QGroupBox>
#include <QDialogButtonBox>
#include <QScrollArea>
#include <QGridLayout>
#include <QMessageBox>
#include <QLabel>
#include <complex>
#include "dividedlg.h"
//#include <QDebug>
dividedlg::dividedlg( Document_Interface *doc, QString passedData,
QWidget *parent ) : QDialog( parent )
{
//hide titie bar '?'
this->setWindowFlags( this->windowFlags() & ~Qt::WindowContextHelpButtonHint );
QFont font1, font2, font3, font4;
font1.setPointSize( 14 );
font2.setPointSize( 12 );
font3.setPointSize( 11 );
font4.setPointSize( 9 );
d = doc; //Document_Interface
QList<QString> data = ( passedData.split
( QRegularExpression ( "[\\n\\t\\r]" ) ) );
QList<QString> splitList = data.at( 0 ).split( ": " );
QString entType = splitList.at( 1 );
this->setWindowTitle( tr("Divide - ") + entType );
entType = entType.toLower();
QHBoxLayout *mainLayout = new QHBoxLayout;
setLayout(mainLayout);
QVBoxLayout *leftcolayout = new QVBoxLayout;
mainLayout->addLayout(leftcolayout);
QFrame *Fr1 = new QFrame();
Fr1->setFrameStyle( QFrame::Box );
Fr1->setLineWidth( 1 );
leftcolayout->addWidget(Fr1);
QFrame *Fr2 = new QFrame();
Fr2->setFrameStyle( QFrame::Box );
Fr2->setLineWidth( 1 );
leftcolayout->addWidget(Fr2);
QGroupBox *G1 = new QGroupBox();
G1->setFont( font2 );
G1->setTitle(tr("Layers"));
G1->setVisible( true );
leftcolayout->addWidget(G1);
QDialogButtonBox *BB1 = new QDialogButtonBox();
leftcolayout->addWidget(BB1, 0, Qt::AlignRight);
BB1->addButton( tr( "OK" ), QDialogButtonBox::AcceptRole );
BB1->addButton( tr( "Cancel" ), QDialogButtonBox::RejectRole );
QObject::connect( BB1, SIGNAL( accepted() ), this, SLOT( onOkClickedSlot() ) );
QObject::connect( BB1, SIGNAL( rejected() ), this, SLOT( reject() ) );
QHBoxLayout* fr1layout = new QHBoxLayout;
Fr1->setLayout(fr1layout);
QLabel *L1 = new QLabel();
fr1layout->addWidget(L1);
L1->setFont( font1 );
L1->setText(tr("Divide at"));
QHBoxLayout* fr2layout = new QHBoxLayout;
Fr2->setLayout(fr2layout);
QLabel *L2 = new QLabel();
fr2layout->addWidget(L2);
L2->setFont( font1 );
L2->setText(tr("Size"));
Sp1 = new QSpinBox(); // quantity
fr1layout->addWidget(Sp1);
Sp1->setFont( font1 );
Sp1->setMaximum( 99 );
Sp1->setMinimum( 0 );
Sp1->setValue( 0 );
QObject::connect( Sp1, SIGNAL( valueChanged( int ) ), this,
SLOT( onQtyChangedSlot( int ) ) );
Sp2 = new QSpinBox(); // size
fr2layout->addWidget(Sp2);
Sp2->setFont(font1);
Sp2->setMaximum( 10 );
Sp2->setMinimum( 1 );
Sp2->setValue(5);
QObject::connect( Sp2, SIGNAL( valueChanged( int ) ), this,
SLOT( onSizeChangedSlot( int ) ) );
QLabel* L3 = new QLabel();
fr1layout->addWidget(L3);
L3->setFont( font1 );
L3->setText(tr("places"));
fr1layout->addSpacing(10); // otherwise text runs too close to R1 below
QLabel* L4 = new QLabel();
fr2layout->addWidget(L4);
L4->setFont( font1 );
L4->setText("%");
R1 = new QRadioButton(Fr1); // ticks on/off
fr1layout->addWidget(R1);
R1->setFont( font2 );
R1->setText(tr("Ticks - Off"));
ticksShowHideFlag = false;
QObject::connect( R1, SIGNAL( toggled( bool ) ), this,
SLOT( onOffTicksSlot( bool ) ) );
R2 = new QRadioButton(Fr2); // breaks on/off
fr2layout->addWidget(R2);
R2->setFont( font2 );
R2->setText(tr("Breaks - Off"));
breaksOnOffFlag = false;
QObject::connect (R2, SIGNAL( toggled( bool ) ), this,
SLOT( onOffBreaksSlot( bool ) ) );
QList<QString> layerList = doc->getAllLayer();
QString activeLayerName = doc->getCurrentLayer();
QString num;
QGridLayout *laylayout = new QGridLayout;
for( int i = 0; i < layerList.size(); i++ )
{
QRadioButton *RB = new QRadioButton;
RB->setFont( font4 );
num = QString::number( i );
RB->setObjectName( num );
RB->setText( num + " - " + layerList.at( i ) );
QObject::connect( RB, SIGNAL( toggled( bool ) ), this,
SLOT( onWhichButtonSlot( bool ) ) );
if ( activeLayerName == layerList.at( i ) )
{
RB->setChecked( true );
activeLayer = i;
}
laylayout->addWidget( RB );
}
QVBoxLayout* g1layout = new QVBoxLayout;
G1->setLayout(g1layout);
QScrollArea *Sa1 = new QScrollArea( G1 );
g1layout->addWidget(Sa1);
Sa1->setLineWidth( 1 );
Sa1->setWidgetResizable(true);
QWidget *containerWidget = new QWidget;
containerWidget->setLayout( laylayout );
Sa1->setWidget( containerWidget );
QHBoxLayout* newlaylayout = new QHBoxLayout;
QLabel *L5 = new QLabel( G1 );
newlaylayout->addWidget(L5);
L5->setFont( font4 );
L5->setText(tr("<i>New</i>"));
Le2 = new QLineEdit( G1 ); // new layer
newlaylayout->addWidget(Le2);
Le2->setFont( font4 );
g1layout->addLayout(newlaylayout);
QVBoxLayout* rtcolayout = new QVBoxLayout;
mainLayout->addLayout(rtcolayout);
if ( entType == "line" )
{
dataToReturn.append( "LINE:" );
rtcolayout->addWidget(choice(STRAIGHT, font4));
} //end line
else if ( entType == "circle" )
{
dataToReturn.append( "CIRCLE:" );
QFrame *Cr2 = new QFrame();
Cr2->setFrameStyle( QFrame::Box );
Cr2->setLineWidth(1);
rtcolayout->addWidget(Cr2);
QVBoxLayout* anglayout = new QVBoxLayout();
Cr2->setLayout(anglayout);
QLabel *C4 = new QLabel();
anglayout->addWidget(C4);
C4->setFont( font4 );
C4->setText(tr("Enter start\nangle, in\ndecimal degrees\n"
"0° at 3 o'clock\ngoes\nanti-clockwise."));
Le1 = new QLineEdit();
anglayout->addWidget(Le1);
QDoubleValidator *doubleVal = new QDoubleValidator
( -999999.999, 999999.999, 3, this ); //± 1 million - 0.001
doubleVal->setNotation( QDoubleValidator::StandardNotation );
Le1->setValidator( doubleVal );
Le1->installEventFilter( this );
QObject::connect( Le1, SIGNAL( textChanged( const QString & ) ), this,
SLOT( onStartAngleChangedSlot ( const QString & ) ) );
rtcolayout->addWidget(choice(CURVED, font4));
} //end circle
else if ( entType == "arc" )
{
dataToReturn.append( "ARC:" );
rtcolayout->addWidget(choice(CURVED, font4));
}
else if ( entType == "polyline" )
{
dataToReturn.append( "POLYLINE:" );
}
rtcolayout->addStretch();
QLabel *C2 = new QLabel();
rtcolayout->addWidget(C2);
C2->setStyleSheet("border: 0.2ex solid black");
C2->setFont(font4);
C2->setContentsMargins( 0, 3, 0, 0 );
C2->setAlignment( Qt::Alignment( Qt::AlignTop ) );
C2->setText(tr("<i>\"New\"</i><br>Enter name<br>for a new layer<br>"
"(if required?) to<br>draw ticks on."));
}
QFrame* dividedlg::choice(ElementKind ek, QFont font )
{
QString msg1 = tr("Ticks");
QString msg2;
QString msg3;
switch ( ek ) {
case STRAIGHT:
msg2 = tr("Above");
msg3 = tr("Below");
break;
case CURVED:
msg2 = tr("Outside");
msg3 = tr("Inside");
break;
default:
break;
}
QFrame *Cr1= new QFrame();
Cr1->setFrameStyle( QFrame::Box );
Cr1->setLineWidth(1);
QVBoxLayout* tickloclayout = new QVBoxLayout;
Cr1->setLayout(tickloclayout);
QLabel *C1 = new QLabel( Cr1 );
tickloclayout->addWidget(C1);
C1->setFont( font );
C1->setContentsMargins( 0, 3, 0, 0 );
C1->setAlignment( Qt::Alignment( Qt::AlignTop ) );
C1->setText( msg1 );
QHBoxLayout* tickchoiceslayout = new QHBoxLayout;
tickloclayout->addLayout(tickchoiceslayout);
QRadioButton *R1 = new QRadioButton( C1 );
tickchoiceslayout->addWidget(R1);
R1->setText( msg2 );
R1->setObjectName( "o" );
QObject::connect(R1,SIGNAL( toggled( bool ) ), this,
SLOT( onInOutSlot( bool ) ) );
R1->setChecked( true );
QRadioButton *R2 = new QRadioButton( C1 );
tickchoiceslayout->addWidget(R2);
R2->setText( msg3 );
R2->setObjectName( "i" );
QObject::connect(R2, SIGNAL( toggled( bool ) ), this,
SLOT( onInOutSlot( bool ) ) );
return Cr1;
}
bool dividedlg::eventFilter( QObject * obj, QEvent * event )
{
if ( Le1->hasFocus() ) //QlineEdit - start angle
{
backSpace = true; //see onStartAngleChangedSlot
if ( event->type() == QEvent::KeyPress )
{
QKeyEvent * keyEvent = static_cast<QKeyEvent *>( event );
if ( keyEvent->key() == Qt::Key_Backspace )
backSpace = false;
}
}
return ( QWidget::eventFilter( obj, event ) );
}
//auto add decinal point after 6 digits
void dividedlg::onStartAngleChangedSlot( const QString & passed )
{
if ( backSpace ) //allows backspace on len = 6 or -7 & decimal point
{ //see eventFilter
int len = passed.length();
bool noPoint = ( ! passed.contains( "." ) );
bool addPoint = false;
if (QChar('-') == passed.at( 0)) { //minus
if ( ( len == 7 ) && noPoint ) addPoint = true;
}
else
if ( ( len == 6 ) && noPoint ) addPoint = true;
if ( addPoint ) Le1->setText( Le1->text() + "." );
}
}
void dividedlg::onInOutSlot( bool state )
{
QObject *object = QObject::sender();
QRadioButton *btn = qobject_cast<QRadioButton *>( object );
inOut = ( state && btn->objectName() == "o" ) ? true : false;
}
void dividedlg::onSizeChangedSlot( int s )
{
size = s;
}
void dividedlg::onQtyChangedSlot( int q )
{
qty = q;
}
void dividedlg::onOffTicksSlot( bool state )
{
state ? R1->setText( "Ticks - On" ) : R1->setText( "Ticks - Off" );
ticksShowHideFlag = state;
}
void dividedlg::onOffBreaksSlot( bool state )
{
state ? R2->setText( "Breaks - On" ) : R2->setText( "Breaks - Off" );
breaksOnOffFlag = state;
}
void dividedlg::onWhichButtonSlot( bool state )
{
QObject *object = QObject::sender();
QRadioButton *btn = qobject_cast<QRadioButton *>(object);
if ( state )
{
d->setLayer( ( btn->text() ).split( "- " ).at( 1 ) );
btn->setText( btn->text() + " (ticks)" );
activeLayer = btn->objectName().toInt();
}
else
btn->setText( btn->text().remove( " (ticks)" ) );
}
void dividedlg::onOkClickedSlot() //OK quit dividedlg
{
dataToReturn.append( QString::number( Sp1->value() ).append(":") ); //ticks
dataToReturn.append( QString::number( Sp2->value() ).append(":") ); //size
dataToReturn.append( R1->isChecked() ? "t:" : "f:" ); //ticks on/off
dataToReturn.append( R2->isChecked() ? "t:" : "f:" ); //breaks on/off
if ( dataToReturn.startsWith( "CI" ) ) //circle
dataToReturn.append( Le1->text().simplified().append( ":" ) );
dataToReturn.append( inOut ? "o:" : "i:" ); //Ternary Operator '?'
QString test = Le2->text().simplified(); //QLineEdit, new layer name
dataToReturn.append( test.isEmpty() ? ( d->getCurrentLayer()
.remove( " (ticks)" ).append( ":" ) )
: ( test.append( ":" ).append( "lay" ) ) );
emit returnData( dataToReturn );
this->accept();
}
dividedlg::~dividedlg()
{
this->deleteLater();
}
|