With NSString, How to catelog a new interface to replace DrawInRect - nsstring

I wrote an interface "myDrawInRect" to replace the orginal "drawInRect" in NSString.
the code is like following:
.h file:
#interface NSString(TextDrawing)
-(CGSize) myDrawInRect:(CGRect)rect withFont:(UIFont *)font textColor:(UIColor*)textColor
lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment;
#end
.m file:
#interface NSString(TextDrawing)
-(CGSize) myDrawInRect:(CGRect)rect withFont:(UIFont *)font textColor:(UIColor*)textColor
lineBreakMode:(NSLineBreakMode)lineBreakMode alignment:(NSTextAlignment)alignment
{
CGFloat screen_scale = [UIScreen mainScreen].scale;
CGContextRef bitmapContext = CreateBitmapContext( rect.size.width * screen_scale, rect.size.height * screen_scale );
/* ... some code to draw the bitmapContext; */
CGImageRef image = CGBitmapContextCreateImage( bitmapContext );
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextScaleCTM( context, 1.0f, -1.0f );
CGContextTranslateCTM( context, 0.0f, -CGRectGetHeight(realRect) );
CGContextDrawImage( context, rect, image );
CGContextRelease( bitmapContext );
CGImageRelease( myImage );
}
#end
I invoked myDrawInRect in UILabel drawInRect, but nothing I can see on the label.
Can anyone advise? thanks!

Try this
CGImageRef image = CGBitmapContextCreateImage( bitmapContext );
CGContextRef context = UIGraphicsGetCurrentContext();
// MODIFY AND ADD FOLLOWING LINES
CGContextTranslateCTM( context, 0.0f, CGRectGetMaxY(realRect)*2 );
CGContextScaleCTM( context, 1.0f, -1.0f );
CGContextDrawImage( context, rect, image );
CGContextScaleCTM( context, 1.0f, -1.0f );
CGContextTranslateCTM( context, 0.0f, -CGRectGetMaxY(realRect)*2 );
// MODIFY UPTO THIS PONIT
CGContextRelease( bitmapContext );
CGImageRelease( myImage );

Related

How to hide action button when QLineEdit is disabled?

I have a class which is inherited from QLineEdit, and I set an icon as an action button for this.
MyEdit::MyEdit( QWidget *p_parent ) : QLineEdit( p_parent )
{
m_buttonAction = addAction( QIcon( "search.png" ), QLineEdit::TrailingPosition );
QAbstractButton *button = qobject_cast<QAbstractButton *>( m_buttonAction->associatedWidgets().last() );
m_buttonAction->setVisible( false );
connect( m_buttonAction, &QAction::triggered, this, &MyEdit::openCompleter );
m_completer = new QCompleter( this );
m_sourceModel = new CompleterSourceModel( m_completer );
m_view = new CompleterView();
m_view->setStyle( &m_style );
m_delegate = new CompleterDelegate( m_view );
m_completer->setPopup( m_view );
m_completer->setModel( m_sourceModel );
m_view->setItemDelegate( m_delegate );
setCompleter( m_completer );
}
void MyEdit::setDataForCompleter( const CompleterData &p_data )
{
m_sourceModel->setCompleterData( p_data );
m_buttonAction->setVisible( p_data.data().size() > 0 );
}
When I import data for completer, the icon is always shown. Now I need to hide this icon in case MyEdit is disabled or as ReadOnly.
I am thinking about override setDisabled and setReadOnly for my class, and in there setVisible for the icon. But these functions are not virtual, so can not be overridden.
I am thinking also about a signal like stateChanged of my class, so I can do it in a slot. But I can not find any signal like that for QLineEdit. Do you have any idea how to do it?
You can handle events QEvent::ReadOnlyChange or QEvent::EnabledChange by overriding the QLineEdit::event method
UPDATE:
Here is an example implementation:
bool MyEdit::event(QEvent *e) override {
const auto type = e->type();
if (type == QEvent::ReadOnlyChange || type == QEvent::EnabledChange) {
m_buttonAction->setVisible(m_sourceModel->rowCount() > 0 ? isEnabled() && isReadOnly() : false);
}
return QLineEdit::event(e);
}

Create and Monitor geofences using polygon shape

I am trying to implement a Geofencing system using polygon shape. Basically if an user enters the geofencing region, the user should get a notification. After going through many research i was only able to find Geofence using Circular. So far i implemented the system but it only monitors by circular shape not if someone enters the polygon drawn in the map. If any has done Polygon Geonfencing before please help me out
This is the code i used to draw my polygon
private void drawGeofence() {
Log.d(TAG, "drawGeofence()");
polyLatLng = new ArrayList<>( );
polyLatLng.add( new LatLng( 6.895450, 79.852170 ) ); // Should match last point
polyLatLng.add( new LatLng(6.897287, 79.859544));
polyLatLng.add( new LatLng( 6.905271, 79.862609 ) );
polyLatLng.add( new LatLng( 6.906114, 79.858998 ) );
polyLatLng.add( new LatLng( 6.911808, 79.856206 ) );
polyLatLng.add( new LatLng( 6.912200, 79.851381 ) );
polyLatLng.add( new LatLng( 6.911627, 79.849621 ) );
polyLatLng.add( new LatLng( 6.910965, 79.848073 ) );
polyLatLng.add( new LatLng( 6.895450, 79.852170 ) ); // Should match first point
Log.i(TAG, "computeArea " + SphericalUtil.computeArea(polyLatLng));
map.addPolygon(new PolygonOptions()
.addAll(polyLatLng)
.strokeColor(Color.BLACK)
.strokeWidth( 4 )
.fillColor(0x220000FF));
}
Here's my geofence code which track only in circular region
private static final float GEOFENCE_RADIUS = 1006.3975694699f; // in meters
private void startGeofence() {
Log.i(TAG, "startGeofence()");
if( geoFenceMarker != null ) {
// create geofence
Geofence geofence = createGeofence( 6.904254, 79.853798, GEOFENCE_RADIUS );
GeofencingRequest geofenceRequest = createGeofenceRequest( geofence );
addGeofence( geofenceRequest );
} else {
Log.e(TAG, "Geofence marker is null");
}
}
// Create a Geofence
private Geofence createGeofence( double lat, double lng, float radius ) {
Log.d(TAG, "createGeofence");
return new Geofence.Builder()
.setRequestId(GEOFENCE_REQ_ID)
.setCircularRegion( lat, lng, radius)
.setExpirationDuration( GEO_DURATION )
.setTransitionTypes( Geofence.GEOFENCE_TRANSITION_ENTER
| Geofence.GEOFENCE_TRANSITION_EXIT )
.build();
}enter code here
Could you use this function from geopandas : https://geopandas.org/en/stable/docs/reference/api/geopandas.GeoSeries.union.html
If you isolate each polygon of your Multipolygon and merge them with this it should work isn't it ?

Move camera backwards and forwards in Three js

How would you move a camera backwards and forwards from a fixed point along the trajectory that it is facing?
I know there are several control scripts that do this but I need to do something custom and I'm not able to break down their code to figure how to isolate the above behaviour.
I've seen this answer which I think addresses the question and have come up with this code:
cameraPosition = camera.position
cameraRotation = new THREE.Vector3(camera.rotation._x, camera.rotation._y, camera.rotation._z)
newCamera = new THREE.Vector3().addVectors(cameraPosition, cameraRotation)
camera.position.set(newCamera.x, newCamera.y, newCamera.z)
camera.updateProjectionMatrix()
But this seems to move the camera in a circle rather than backwards and forwards.
Any help would be much appreciated. Thank you!
To move the camera forward or backward the direction it is facing, use
camera.translateZ( - distance );
or
camera.translateZ( distance );
three.js r.78
Here's how you do it by updating the camera.position.z. Use the W=forward, S=backward
var camera, scene, renderer, geometry, material, mesh;
init();
animate();
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 1, 10000);
camera.position.z = 500;
scene.add(camera);
geometry = new THREE.CubeGeometry(200, 200, 200);
material = new THREE.MeshNormalMaterial();
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
document.body.addEventListener( 'keydown', onKeyDown, false );
}
function animate() {
requestAnimationFrame(animate);
render();
}
function render() {
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
renderer.render(scene, camera);
}
function onKeyDown(){
switch( event.keyCode ) {
case 83: // up
camera.position.z += 50;
break;
case 87: // down
camera.position.z -= 50;
break;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/threejs/r76/three.min.js"></script>

QHeaderView::mousePressEvent (need to figure out the column/index)

Generally I can get this to work no problem when I reimplement QTableView::mousePressEvent( QMouseEvent* ). However, doing it on QHeaderView is not working for me. Code is simple.
void my_header_t::mousePressEvent( QMouseEvent* event )
{
if ( !event ) {
return;
}
if ( event->button() == Qt::RightButton ) {
QPoint point( event->x(), event->y() );
QModelIndex index = indexAt( point );
printf( "%s data %s %d,%d %s (point: %d,%d )\n",
ts().c_str(), index.data().toString().toStdString().c_str(),
index.row(), index.column(), index.isValid() ? "True" : "False",
event->x(), event->y() );
handle_right_click( index.data().toString() );
} else {
QHeaderView::mousePressEvent( event );
}
x() and y() from the QMouseEvent are fine. However, it creates an invalid index, with row() of -1, and column() of -1. Obviously, I'm passing an empty string to handle_right_click() which kicks off a menu. That menu is not going to know which column called it, and the mayhem will further ensue.
I know that clicked( const QModelIndex& ) will just tell me the right index, with the text. However, I need to differentiate between buttons.
QHeaderView provides an alternative function, logicalIndexAt, for determining the index of the header item that you're interested in. Using your code from above:
void my_header_t::mousePressEvent( QMouseEvent* event )
{
if ( !event ) {
return;
}
if ( event->button() == Qt::RightButton ) {
int index = logicalIndexAt( event->pos() );
handle_right_click(model()->headerData(index, Qt::Horizontal).toString());
} else {
QHeaderView::mousePressEvent( event );
}
}
Note that the orientation of the header must be passed to the headerData method (in this case, I've just assumed that it's Qt::Horizontal, but in your case it might be something different).

How to use GetHBITMAP method?

I have a gdi+ bitmap, and I want to convert bitmap into HBitmap.
I write the following code.
HBITMAP temp;
Color color;
img->GetHBITMAP(color, &temp);
But It do not work, How can I get a HBitmap?
Demonstration code from MSDN:
void DemonstrateGetHbitmapWithColor()
{
Bitmap^ bm = gcnew Bitmap( "Picture.jpg" );
IntPtr hBitmap = bm->GetHbitmap( Color::Blue );
// Do something with hBitmap.
DeleteObject( hBitmap );
}
Check the return value of the GetHBITMAP function.

Resources