Skip to content
Permalink
Browse files

[GRASS] hide db driver window on Windows

  • Loading branch information
blazek committed Oct 22, 2015
1 parent f5b0dbe commit 54e2a5112a46b81059a565bc7df5e918645eeb90
@@ -39,6 +39,7 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)

SET (GRASS_LIBRARY_SRCS
../qgsgrass.cpp
../qgsgrasswin.cpp
../qgsgrassdatafile.cpp
../qgsgrassfeatureiterator.cpp
../qgsgrassimport.cpp
@@ -14,11 +14,13 @@
* *
***************************************************************************/


#include <QFileInfo>

#include "qgslogger.h"

#include "qgsgrass.h"
#include "qgsgrasswin.h"
#include "qgsgrassvectormap.h"
#include "qgsgrassvectormaplayer.h"

@@ -111,26 +113,17 @@ void QgsGrassVectorMapLayer::load()
QFileInfo di( mMap->grassObject().mapsetPath() + "/vector/" + mMap->grassObject().name() + "/dbln" );
mLastLoaded = di.lastModified();

QgsGrass::lock();
dbDriver *databaseDriver = 0;
QString error = QString( "Cannot open database %1 by driver %2" ).arg( mFieldInfo->database, mFieldInfo->driver );
G_TRY
{
setMapset();
databaseDriver = db_start_driver_open_database( mFieldInfo->driver, mFieldInfo->database );
}
G_CATCH( QgsGrass::Exception &e )
{
QgsGrass::warning( error + " : " + e.what() );
}
QString error;
dbDriver *databaseDriver = openDriver( error );

if ( !databaseDriver )
if ( !databaseDriver || !error.isEmpty() )
{
QgsDebugMsg( error );
}
else
{
QgsDebugMsg( "Database opened -> open select cursor" );
QgsGrass::lock(); // not sure if lock is necessary
dbString dbstr;
db_init_string( &dbstr );
db_set_string( &dbstr, ( char * )"select * from " );
@@ -286,8 +279,8 @@ void QgsGrassVectorMapLayer::load()
QgsDebugMsg( QString( "mTableFields.size = %1" ).arg( mTableFields.size() ) );
QgsDebugMsg( QString( "number of attributes = %1" ).arg( mAttributes.size() ) );
}
QgsGrass::unlock();
}
QgsGrass::unlock();
}

// Add cat if no attribute fields exist (otherwise qgis crashes)
@@ -427,6 +420,7 @@ dbDriver * QgsGrassVectorMapLayer::openDriver( QString &error )
{
QgsDebugMsg( "entered" );
dbDriver * driver = 0;

if ( !mFieldInfo )
{
error = tr( "No field info" );
@@ -435,17 +429,33 @@ dbDriver * QgsGrassVectorMapLayer::openDriver( QString &error )
else
{
QgsDebugMsg( "Field info found -> open database" );
setMapset();
driver = db_start_driver_open_database( mFieldInfo->driver, mFieldInfo->database );

if ( !driver )
QString err = QString( "Cannot open database %1 by driver %2" ).arg( mFieldInfo->database, mFieldInfo->driver );
QgsGrass::lock();
G_TRY
{
setMapset();
driver = db_start_driver_open_database( mFieldInfo->driver, mFieldInfo->database );
if ( !driver )
{
error = err;
QgsDebugMsg( error );
}
}
G_CATCH( QgsGrass::Exception &e )
{
error = tr( "Cannot open database %1 by driver %2" ).arg( mFieldInfo->database, mFieldInfo->driver );
error = err + " : " + e.what();
QgsDebugMsg( error );
}
else
QgsGrass::unlock();

if ( driver )
{
QgsDebugMsg( "Database opened" );
#ifdef Q_OS_WIN
// Driver on Windows opens black window:
// https://lists.osgeo.org/pipermail/grass-dev/2015-October/076831.html
QgsGrassWin::hideWindow( driver->pid );
#endif
}
}
return driver;
@@ -0,0 +1,73 @@
/***************************************************************************
qgsgrasswin.cpp
-------------------
begin : October, 2015
copyright : (C) 2015 by Radim Blazek
email : radim.blazek@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it 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. *
* *
***************************************************************************/

#include <QtGlobal>

#include "qgslogger.h"
#include "qgsgrasswin.h"

#ifdef Q_OS_WIN
#include "Windows.h"
#include "WinDef.h"
#include "Winuser.h"
#endif

#ifdef Q_OS_WIN
// Ideas/code from http://stackoverflow.com/questions/20162359/c-best-way-to-get-window-handle-of-the-only-window-from-a-process-by-process
// Get window for pid
struct EnumData {
DWORD dwProcessId;
HWND hWnd;
};
BOOL CALLBACK EnumProc( HWND hWnd, LPARAM lParam ) {
EnumData& ed = *(EnumData*)lParam;
DWORD dwProcessId = 0x0;
GetWindowThreadProcessId( hWnd, &dwProcessId );
if ( ed.dwProcessId == dwProcessId ) {
ed.hWnd = hWnd;
SetLastError( ERROR_SUCCESS );
return FALSE;
}
return TRUE;
}
HWND FindWindowFromProcessId( DWORD dwProcessId ) {
EnumData ed = { dwProcessId };
if ( !EnumWindows( EnumProc, (LPARAM)&ed ) &&
( GetLastError() == ERROR_SUCCESS ) ) {
return ed.hWnd;
}
return NULL;
}
#endif

void QgsGrassWin::hideWindow( int pid )
{
Q_UNUSED(pid)
QgsDebugMsg( QString("pid = %1").arg(pid) );
#ifdef Q_OS_WIN
HWND hWnd = FindWindowFromProcessId( (DWORD)pid );
if ( hWnd )
{
QgsDebugMsg( "driver window found -> minimize" );
}
else
{
QgsDebugMsg( "cannot find driver window" );
}
// Unfortunately the window opens first for a moment
ShowWindow( hWnd, SW_HIDE);
#endif
}
@@ -0,0 +1,27 @@
/***************************************************************************
qgsgrasswin.h
-------------------
begin : October, 2015
copyright : (C) 2015 by Radim Blazek
email : radim.blazek@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it 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. *
* *
***************************************************************************/

#ifndef QGSGRASSWIN_H
#define QGSGRASSWIN_H

// Windows utils
class GRASS_LIB_EXPORT QgsGrassWin
{
public:
static void hideWindow( int pid );
};

#endif // QGSGRASSWIN_H

0 comments on commit 54e2a51

Please sign in to comment.
You can’t perform that action at this time.