Skip to content

Commit

Permalink
Gui: Use auto and range-based for (#7481)
Browse files Browse the repository at this point in the history
* On lines where the variable type is obvious from inspection, avoid repeating the type using auto. 
* When possible use a ranged for loop instead of begin() and end() iterators
  • Loading branch information
berniev committed Sep 14, 2022
1 parent d779282 commit 75acacd
Show file tree
Hide file tree
Showing 175 changed files with 2,052 additions and 2,058 deletions.
2 changes: 1 addition & 1 deletion src/Gui/3Dconnexion/GuiNativeEventMac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Gui::GuiNativeEvent::tdx_drv_handler(io_connect_t connection,
//printf("tdx_drv_handler\n");
//printf("connection: %X\n", connection);
//printf("messageType %c%c%c%c\n", messageType/0x1000000, messageType/0x10000, messageType/0x100, messageType);
ConnexionDeviceStatePtr msg = (ConnexionDeviceStatePtr)messageArgument;
auto msg = (ConnexionDeviceStatePtr)messageArgument;

switch(messageType) {
case kConnexionMsgDeviceState:
Expand Down
22 changes: 11 additions & 11 deletions src/Gui/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ void ActionGroup::addTo(QWidget *w)
// and adding this action to the widget doesn't work.
if (_dropDown) {
if (w->inherits("QMenu")) {
QMenu *menu = new QMenu(w);
auto menu = new QMenu(w);
QAction* action = qobject_cast<QMenu*>(w)->addMenu(menu);
action->setMenuRole(_action->menuRole());
menu->setTitle(_action->text());
Expand All @@ -259,7 +259,7 @@ void ActionGroup::addTo(QWidget *w)
tb->setPopupMode(QToolButton::MenuButtonPopup);
tb->setObjectName(QString::fromLatin1("qt_toolbutton_menubutton"));
QList<QAction*> acts = _group->actions();
QMenu* menu = new QMenu(tb);
auto menu = new QMenu(tb);
menu->addActions(acts);
tb->setMenu(menu);
//tb->addActions(_group->actions());
Expand Down Expand Up @@ -468,7 +468,7 @@ void WorkbenchComboBox::onActivated(int i)
{
// Send the event to the workbench group to delay the destruction of the emitting widget.
int index = itemData(i).toInt();
WorkbenchActionEvent* ev = new WorkbenchActionEvent(this->actions().at(index));
auto ev = new WorkbenchActionEvent(this->actions().at(index));
QApplication::postEvent(this->group, ev);
// TODO: Test if we can use this instead
//QTimer::singleShot(20, this->actions()[i], SLOT(trigger()));
Expand Down Expand Up @@ -529,7 +529,7 @@ void WorkbenchGroup::addTo(QWidget *w)
{
refreshWorkbenchList();
if (w->inherits("QToolBar")) {
QToolBar* bar = qobject_cast<QToolBar*>(w);
auto bar = qobject_cast<QToolBar*>(w);
QComboBox* box = new WorkbenchComboBox(this, w);
box->setIconSize(QSize(16, 16));
box->setToolTip(_action->toolTip());
Expand All @@ -540,7 +540,7 @@ void WorkbenchGroup::addTo(QWidget *w)
bar->addWidget(box);
}
else if (w->inherits("QMenu")) {
QMenu* menu = qobject_cast<QMenu*>(w);
auto menu = qobject_cast<QMenu*>(w);
menu = menu->addMenu(_action->text());
menu->addActions(_group->actions());
}
Expand Down Expand Up @@ -613,7 +613,7 @@ void WorkbenchGroup::refreshWorkbenchList()
void WorkbenchGroup::customEvent( QEvent* e )
{
if (e->type() == QEvent::User) {
Gui::WorkbenchActionEvent* ce = (Gui::WorkbenchActionEvent*)e;
auto ce = (Gui::WorkbenchActionEvent*)e;
ce->action()->trigger();
}
}
Expand Down Expand Up @@ -826,8 +826,8 @@ void RecentFilesAction::restore()
_group->addAction(QLatin1String(""))->setVisible(false);
std::vector<std::string> MRU = hGrp->GetASCIIs("MRU");
QStringList files;
for (std::vector<std::string>::iterator it = MRU.begin(); it!=MRU.end();++it)
files.append(QString::fromUtf8(it->c_str()));
for(const auto& it : MRU)
files.append(QString::fromUtf8(it.c_str()));
setFiles(files);
}

Expand Down Expand Up @@ -969,9 +969,9 @@ void RecentMacrosAction::activateFile(int id)
}
else {
if (QApplication::keyboardModifiers() == Qt::ShiftModifier){ //open for editing on Shift+click
PythonEditor* editor = new PythonEditor();
auto editor = new PythonEditor();
editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
PythonEditorView* edit = new PythonEditorView(editor, getMainWindow());
auto edit = new PythonEditorView(editor, getMainWindow());
edit->setDisplayName(PythonEditorView::FileName);
edit->open(filename);
edit->resize(400, 300);
Expand Down Expand Up @@ -1212,7 +1212,7 @@ WindowAction::~WindowAction()

void WindowAction::addTo ( QWidget * w )
{
QMenu* menu = qobject_cast<QMenu*>(w);
auto menu = qobject_cast<QMenu*>(w);
if (!menu) {
if (!_menu) {
_menu = new QMenu();
Expand Down
6 changes: 3 additions & 3 deletions src/Gui/ActionFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void ActionFunction::triggered()
{
Q_D(ActionFunction);

QAction* a = qobject_cast<QAction*>(sender());
auto a = qobject_cast<QAction*>(sender());
QMap<QAction*, std::function<void()> >::iterator it = d->triggerMap.find(a);
if (it != d->triggerMap.end()) {
// invoke the class function here
Expand All @@ -85,7 +85,7 @@ void ActionFunction::toggled(bool on)
{
Q_D(ActionFunction);

QAction* a = qobject_cast<QAction*>(sender());
auto a = qobject_cast<QAction*>(sender());
QMap<QAction*, std::function<void(bool)> >::iterator it = d->toggleMap.find(a);
if (it != d->toggleMap.end()) {
// invoke the class function here
Expand All @@ -105,7 +105,7 @@ void ActionFunction::hovered()
{
Q_D(ActionFunction);

QAction* a = qobject_cast<QAction*>(sender());
auto a = qobject_cast<QAction*>(sender());
QMap<QAction*, std::function<void()> >::iterator it = d->hoverMap.find(a);
if (it != d->hoverMap.end()) {
// invoke the class function here
Expand Down
30 changes: 15 additions & 15 deletions src/Gui/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ FreeCADGui_subgraphFromObject(PyObject * /*self*/, PyObject *args)
std::string vp = obj->getViewProviderName();
SoNode* node = nullptr;
try {
Base::BaseClass* base = static_cast<Base::BaseClass*>(Base::Type::createInstanceByName(vp.c_str(), true));
auto base = static_cast<Base::BaseClass*>(Base::Type::createInstanceByName(vp.c_str(), true));
if (base && base->getTypeId().isDerivedFrom(Gui::ViewProviderDocumentObject::getClassTypeId())) {
std::unique_ptr<Gui::ViewProviderDocumentObject> vp(static_cast<Gui::ViewProviderDocumentObject*>(base));
std::map<std::string, App::Property*> Map;
Expand All @@ -221,8 +221,8 @@ FreeCADGui_subgraphFromObject(PyObject * /*self*/, PyObject *args)
static_cast<App::PropertyPythonObject*>(pyproxy)->setValue(Py::Long(1));
}

for (std::map<std::string, App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it) {
vp->updateData(it->second);
for (const auto & it : Map){
vp->updateData(it.second);
}

std::vector<std::string> modes = vp->getDisplayModes();
Expand Down Expand Up @@ -268,7 +268,7 @@ FreeCADGui_exportSubgraph(PyObject * /*self*/, PyObject *args)
void* ptr = nullptr;
try {
Base::Interpreter().convertSWIGPointerObj("pivy.coin", "SoNode *", proxy, &ptr, 0);
SoNode* node = static_cast<SoNode*>(ptr);
auto node = static_cast<SoNode*>(ptr);
if (node) {
std::string formatStr(format);
std::string buffer;
Expand Down Expand Up @@ -695,7 +695,7 @@ void Application::exportTo(const char* FileName, const char* DocName, const char
std::stringstream str;
std::set<App::DocumentObject*> unique_objs;
str << "__objs__=[]" << std::endl;
for (std::vector<App::DocumentObject*>::iterator it = sel.begin(); it != sel.end(); ++it) {
for (auto it = sel.begin(); it != sel.end(); ++it) {
if (unique_objs.insert(*it).second) {
str << "__objs__.append(FreeCAD.getDocument(\"" << DocName << "\").getObject(\""
<< (*it)->getNameInDocument() << "\"))" << std::endl;
Expand Down Expand Up @@ -760,7 +760,7 @@ void Application::slotNewDocument(const App::Document& Doc, bool isMainDoc)
std::map<const App::Document*, Gui::Document*>::const_iterator it = d->documents.find(&Doc);
assert(it==d->documents.end());
#endif
Gui::Document* pDoc = new Gui::Document(const_cast<App::Document*>(&Doc),this);
auto pDoc = new Gui::Document(const_cast<App::Document*>(&Doc),this);
d->documents[&Doc] = pDoc;

// connect the signals to the application for the new document
Expand Down Expand Up @@ -1552,9 +1552,9 @@ QStringList Application::workbenches() const
{
// If neither 'HiddenWorkbench' nor 'ExtraWorkbench' is set then all workbenches are returned.
const std::map<std::string,std::string>& config = App::Application::Config();
std::map<std::string, std::string>::const_iterator ht = config.find("HiddenWorkbench");
std::map<std::string, std::string>::const_iterator et = config.find("ExtraWorkbench");
std::map<std::string, std::string>::const_iterator st = config.find("StartWorkbench");
auto ht = config.find("HiddenWorkbench");
auto et = config.find("ExtraWorkbench");
auto st = config.find("StartWorkbench");
const char* start = (st != config.end() ? st->second.c_str() : "<none>");
QStringList hidden, extra;
if (ht != config.end()) {
Expand Down Expand Up @@ -1914,8 +1914,8 @@ void Application::runApplication()
// opens them
QDir cwd = QDir::current();
std::list<std::string> files = App::Application::getCmdLineFiles();
for (std::list<std::string>::iterator jt = files.begin(); jt != files.end(); ++jt) {
QString fn = QString::fromUtf8(jt->c_str(), static_cast<int>(jt->size()));
for (const auto & file : files) {
QString fn = QString::fromUtf8(file.c_str(), static_cast<int>(file.size()));
QFileInfo fi(fn);
// if path name is relative make it absolute because the running instance
// cannot determine the full path when trying to load the file
Expand Down Expand Up @@ -2041,13 +2041,13 @@ void Application::runApplication()

// filter wheel events for combo boxes
if (hGrp->GetBool("ComboBoxWheelEventFilter", false)) {
WheelEventFilter* filter = new WheelEventFilter(&mainApp);
auto filter = new WheelEventFilter(&mainApp);
mainApp.installEventFilter(filter);
}

//filter keyboard events to substitute decimal separator
if (hGrp->GetBool("SubstituteDecimalSeparator", false)) {
KeyboardFilter* filter = new KeyboardFilter(&mainApp);
auto filter = new KeyboardFilter(&mainApp);
mainApp.installEventFilter(filter);
}

Expand Down Expand Up @@ -2112,7 +2112,7 @@ void Application::runApplication()
}
QPixmap px(path);
if (!px.isNull()) {
QLabel* logo = new QLabel();
auto logo = new QLabel();
logo->setPixmap(px.scaledToHeight(32));
mw.statusBar()->addPermanentWidget(logo, 0);
logo->setFrameShape(QFrame::NoFrame);
Expand Down Expand Up @@ -2290,7 +2290,7 @@ void Application::runApplication()
void Application::setStyleSheet(const QString& qssFile, bool tiledBackground)
{
Gui::MainWindow* mw = getMainWindow();
QMdiArea* mdi = mw->findChild<QMdiArea*>();
auto mdi = mw->findChild<QMdiArea*>();
mdi->setProperty("showImage", tiledBackground);

// Qt's style sheet doesn't support it to define the link color of a QLabel
Expand Down
16 changes: 8 additions & 8 deletions src/Gui/ApplicationPy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,9 +645,9 @@ PyObject* Application::sOpen(PyObject * /*self*/, PyObject *args)
else if (ext == QLatin1String("py") ||
ext == QLatin1String("fcmacro") ||
ext == QLatin1String("fcscript")) {
PythonEditor* editor = new PythonEditor();
auto editor = new PythonEditor();
editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
PythonEditorView* edit = new PythonEditorView(editor, getMainWindow());
auto edit = new PythonEditorView(editor, getMainWindow());
edit->open(fileName);
edit->resize(400, 300);
getMainWindow()->addWindow( edit );
Expand Down Expand Up @@ -719,9 +719,9 @@ PyObject* Application::sInsert(PyObject * /*self*/, PyObject *args)
else if (ext == QLatin1String("py") ||
ext == QLatin1String("fcmacro") ||
ext == QLatin1String("fcscript")) {
PythonEditor* editor = new PythonEditor();
auto editor = new PythonEditor();
editor->setWindowIcon(Gui::BitmapFactory().iconFromTheme("applications-python"));
PythonEditorView* edit = new PythonEditorView(editor, getMainWindow());
auto edit = new PythonEditorView(editor, getMainWindow());
edit->open(fileName);
edit->resize(400, 300);
getMainWindow()->addWindow( edit );
Expand Down Expand Up @@ -769,7 +769,7 @@ PyObject* Application::sExport(PyObject * /*self*/, PyObject *args)
ext == QLatin1String("xhtml")) {

// build up the graph
SoSeparator* sep = new SoSeparator();
auto sep = new SoSeparator();
sep->ref();

for (Py::Sequence::iterator it = list.begin(); it != list.end(); ++it) {
Expand Down Expand Up @@ -804,7 +804,7 @@ PyObject* Application::sExport(PyObject * /*self*/, PyObject *args)
if (gui_doc) {
Gui::MDIView* view = gui_doc->getActiveView();
if (view) {
View3DInventor* view3d = qobject_cast<View3DInventor*>(view);
auto view3d = qobject_cast<View3DInventor*>(view);
if (view3d)
view3d->viewAll();
QPrinter printer(QPrinter::ScreenResolution);
Expand Down Expand Up @@ -1495,14 +1495,14 @@ PyObject* Application::sCreateViewer(PyObject * /*self*/, PyObject *args)
return nullptr;
}
else if (num_of_views == 1) {
View3DInventor* viewer = new View3DInventor(nullptr, nullptr);
auto viewer = new View3DInventor(nullptr, nullptr);
if (title)
viewer->setWindowTitle(QString::fromUtf8(title));
Gui::getMainWindow()->addWindow(viewer);
return viewer->getPyObject();
}
else {
SplitView3DInventor* viewer = new SplitView3DInventor(num_of_views, nullptr, nullptr);
auto viewer = new SplitView3DInventor(num_of_views, nullptr, nullptr);
if (title)
viewer->setWindowTitle(QString::fromUtf8(title));
Gui::getMainWindow()->addWindow(viewer);
Expand Down
8 changes: 4 additions & 4 deletions src/Gui/AutoSaver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ void AutoSaveProperty::slotNewObject(const App::DocumentObject& obj)

// if an object was deleted and then restored by an undo then add all properties
// because this might be the data files which we may want to re-write
for (std::vector<App::Property*>::iterator it = props.begin(); it != props.end(); ++it) {
slotChangePropertyData(*(*it));
for (const auto & prop : props) {
slotChangePropertyData(*prop);
}
}

Expand Down Expand Up @@ -289,7 +289,7 @@ bool RecoveryWriter::shouldWrite(const std::string& name, const Base::Persistenc
// Property files of a view provider can always be written because
// these are rather small files.
if (object->isDerivedFrom(App::Property::getClassTypeId())) {
const App::Property* prop = static_cast<const App::Property*>(object);
const auto* prop = static_cast<const App::Property*>(object);
const App::PropertyContainer* parent = prop->getContainer();
if (parent && parent->isDerivedFrom(Gui::ViewProvider::getClassTypeId()))
return true;
Expand Down Expand Up @@ -381,7 +381,7 @@ void RecoveryWriter::writeFiles()

// For properties a copy can be created and then this can be written to disk in a thread
if (entry.Object->isDerivedFrom(App::Property::getClassTypeId())) {
const App::Property* prop = static_cast<const App::Property*>(entry.Object);
const auto* prop = static_cast<const App::Property*>(entry.Object);
QThreadPool::globalInstance()->start(new RecoveryRunnable(getModes(), DirName.c_str(), entry.FileName.c_str(), prop));
}
else {
Expand Down
6 changes: 3 additions & 3 deletions src/Gui/AxisOrigin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ SoGroup *AxisOrigin::getNode() {
pCoords->point.setNum(3);
pCoords->point.setValues(0, 13, verts);

SoAutoZoomTranslation *zoom = new SoAutoZoomTranslation;
auto zoom = new SoAutoZoomTranslation;
zoom->scaleFactor = scale;

SoDrawStyle* style = new SoDrawStyle();
auto style = new SoDrawStyle();
style->lineWidth = lineSize;
style->pointSize = pointSize;

SoMaterialBinding* matBinding = new SoMaterialBinding;
auto matBinding = new SoMaterialBinding;
matBinding->value = SoMaterialBinding::PER_FACE_INDEXED;

node->addChild(zoom);
Expand Down
4 changes: 2 additions & 2 deletions src/Gui/AxisOriginPyImp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ PyObject* AxisOriginPy::getElementPicked(PyObject* args)
return nullptr;
}

SoPickedPoint *pp = static_cast<SoPickedPoint*>(ptr);
auto pp = static_cast<SoPickedPoint*>(ptr);
std::string name;
if (!getAxisOriginPtr()->getElementPicked(pp,name))
Py_Return;
Expand All @@ -85,7 +85,7 @@ PyObject* AxisOriginPy::getDetailPath(PyObject* args)
return nullptr;
}

SoPath *pPath = static_cast<SoPath*>(ptr);
auto pPath = static_cast<SoPath*>(ptr);
SoDetail *det = nullptr;
if (!getAxisOriginPtr()->getDetailPath(sub, static_cast<SoFullPath*>(pPath), det)) {
delete det;
Expand Down
8 changes: 4 additions & 4 deletions src/Gui/BitmapFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ void BitmapFactoryInst::restoreCustomPaths()
Base::Reference<ParameterGrp> group = App::GetApplication().GetParameterGroupByPath
("User parameter:BaseApp/Preferences/Bitmaps");
std::vector<std::string> paths = group->GetASCIIs("CustomPath");
for (std::vector<std::string>::iterator it = paths.begin(); it != paths.end(); ++it) {
addPath(QString::fromUtf8(it->c_str()));
for (auto & path : paths) {
addPath(QString::fromUtf8(path.c_str()));
}
}

Expand Down Expand Up @@ -469,12 +469,12 @@ QPixmap BitmapFactoryInst::merge(const QPixmap& p1, const QPixmap& p2, bool vert
QBitmap mask2 = p2.mask();
mask.fill( Qt::color0 );

QPainter* pt1 = new QPainter(&res);
auto* pt1 = new QPainter(&res);
pt1->drawPixmap(0, 0, p1);
pt1->drawPixmap(x, y, p2);
delete pt1;

QPainter* pt2 = new QPainter(&mask);
auto* pt2 = new QPainter(&mask);
pt2->drawPixmap(0, 0, mask1);
pt2->drawPixmap(x, y, mask2);
delete pt2;
Expand Down

0 comments on commit 75acacd

Please sign in to comment.