Skip to content

Commit

Permalink
Some tweaks to zoom combo box
Browse files Browse the repository at this point in the history
Just a typo fix, line length reduction, preferring QVector::last over
QVector::back and removing an unnecessary check on whether a MapView has
a Zoomable instance.
  • Loading branch information
bjorn committed May 17, 2012
1 parent ef5ad07 commit 61a9ffa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/tiled/mainwindow.cpp
Expand Up @@ -107,6 +107,7 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
, mLayerDock(new LayerDock(this))
, mTilesetDock(new TilesetDock(this))
, mCurrentLayerLabel(new QLabel)
, mZoomable(0)
, mZoomComboBox(new QComboBox)
, mStatusInfoLabel(new QLabel)
, mClipboardManager(new ClipboardManager(this))
Expand Down Expand Up @@ -162,7 +163,6 @@ MainWindow::MainWindow(QWidget *parent, Qt::WFlags flags)
tabifyDockWidget(undoDock, mLayerDock);
addDockWidget(Qt::RightDockWidgetArea, mTilesetDock);

mZoomable = 0;
statusBar()->addPermanentWidget(mZoomComboBox);

mUi->actionNew->setShortcuts(QKeySequence::New);
Expand Down Expand Up @@ -1455,8 +1455,8 @@ void MainWindow::mapDocumentChanged(MapDocument *mapDocument)
SLOT(updateActions()));

if (MapView *mapView = mDocumentManager->currentMapView()) {
if ((mZoomable = mapView->zoomable()))
mZoomable->connectToComboBox(mZoomComboBox);
mZoomable = mapView->zoomable();
mZoomable->connectToComboBox(mZoomComboBox);
}
}

Expand Down
46 changes: 27 additions & 19 deletions src/tiled/zoomable.cpp
Expand Up @@ -43,6 +43,13 @@ static const qreal zoomFactors[] = {
};
const int zoomFactorCount = sizeof(zoomFactors) / sizeof(zoomFactors[0]);


static QString scaleToString(qreal scale)
{
return QString(QLatin1String("%1 %")).arg(int(scale * 100));
}


Zoomable::Zoomable(QObject *parent)
: QObject(parent)
, mScale(1)
Expand All @@ -61,14 +68,14 @@ void Zoomable::setScale(qreal scale)

mScale = scale;

synchComboBox();
syncComboBox();

emit scaleChanged(mScale);
}

bool Zoomable::canZoomIn() const
{
return mScale < mZoomFactors.back();
return mScale < mZoomFactors.last();
}

bool Zoomable::canZoomOut() const
Expand Down Expand Up @@ -142,13 +149,15 @@ void Zoomable::connectToComboBox(QComboBox *comboBox)
if (mComboBox) {
mComboBox->clear();
foreach (qreal scale, mZoomFactors)
mComboBox->addItem(QString(QLatin1String("%1 %")).arg(int(scale * 100)), scale);
synchComboBox();
connect(mComboBox, SIGNAL(activated(int)), this, SLOT(comboActivated(int)));
mComboBox->addItem(scaleToString(scale), scale);
syncComboBox();
connect(mComboBox, SIGNAL(activated(int)),
this, SLOT(comboActivated(int)));

mComboBox->setEditable(true);
mComboBox->setInsertPolicy(QComboBox::NoInsert);
connect(mComboBox->lineEdit(), SIGNAL(editingFinished()), this, SLOT(comboEdited()));
connect(mComboBox->lineEdit(), SIGNAL(editingFinished()),
this, SLOT(comboEdited()));

if (!mComboValidator)
mComboValidator = new QRegExpValidator(mComboRegExp, this);
Expand All @@ -158,29 +167,28 @@ void Zoomable::connectToComboBox(QComboBox *comboBox)

void Zoomable::comboActivated(int index)
{
QVariant data = mComboBox->itemData(index);
qreal scale = data.toReal();
setScale(scale);
setScale(mComboBox->itemData(index).toReal());
}

void Zoomable::comboEdited()
{
int pos = mComboRegExp.indexIn(mComboBox->lineEdit()->text());
int pos = mComboRegExp.indexIn(mComboBox->currentText());
Q_ASSERT(pos != -1);

qreal scale = qBound(mZoomFactors.first(),
qreal(mComboRegExp.cap(1).toFloat() / 100.f),
mZoomFactors.back());
qreal(mComboRegExp.cap(1).toDouble() / 100.f),
mZoomFactors.last());

setScale(scale);
}

void Zoomable::synchComboBox()
void Zoomable::syncComboBox()
{
if (mComboBox) {
int index = mComboBox->findData(mScale);
// For a custom scale, the current index must be set to -1
mComboBox->setCurrentIndex(index);
mComboBox->setEditText(QString(QLatin1String("%1 %")).arg(int(mScale * 100)));
}
if (!mComboBox)
return;

int index = mComboBox->findData(mScale);
// For a custom scale, the current index must be set to -1
mComboBox->setCurrentIndex(index);
mComboBox->setEditText(scaleToString(mScale));
}
4 changes: 2 additions & 2 deletions src/tiled/zoomable.h
Expand Up @@ -83,14 +83,14 @@ private slots:
void scaleChanged(qreal scale);

private:
void synchComboBox();
void syncComboBox();

private:
qreal mScale;
QVector<qreal> mZoomFactors;
QComboBox *mComboBox;
QRegExp mComboRegExp;
QRegExpValidator* mComboValidator;
QRegExpValidator *mComboValidator;
};

} // namespace Internal
Expand Down

0 comments on commit 61a9ffa

Please sign in to comment.