I'm writing my own 4 state button and I'm not quite sure what to put in the checkStateSet() method, if anything.
Here is what I've got so far:
SyncDirectionButton::SyncDirectionButton(QWidget *parent) :
QAbstractButton(parent)
{
setCheckable(true);
setToolTip(tr("Click to change the sync direction"));
_state = NoSync;
}
void SyncDirectionButton::paintEvent(QPaintEvent *e)
{
static QPixmapCache::Key noneKey;
static QPixmapCache::Key bothKey;
static QPixmapCache::Key leftKey;
static QPixmapCache::Key rightKey;
QPainter p(this);
QPixmap pix;
if (checkState() == SyncLeft) {
if (!QPixmapCache::find(leftKey, &pix)) {
pix.load(":/icons/sync-left.png");
leftKey = QPixmapCache::insert(pix);
}
} else if (checkState() == SyncBoth) {
if (!QPixmapCache::find(rightKey, &pix)) {
pix.load(":/icons/sync-right.png");
rightKey = QPixmapCache::insert(pix);
}
} else if (checkState() == SyncRight) {
if (!QPixmapCache::find(bothKey, &pix)) {
pix.load(":/icons/sync-both.png");
bothKey = QPixmapCache::insert(pix);
}
} else if (checkState() == NoSync) {
if (!QPixmapCache::find(noneKey, &pix)) {
pix.load(":/icons/application-exit.png");
noneKey = QPixmapCache::insert(pix);
}
}
p.drawPixmap(0,0,pix);
}
SyncDirectionButton::DirectionState SyncDirectionButton::checkState() const
{
return _state;
}
void SyncDirectionButton::setCheckState(DirectionState state)
{
setChecked(state != NoSync);
if (state != _state) {
_state = state;
}
}
QSize SyncDirectionButton::sizeHint() const
{
return QSize(180,90);
}
void SyncDirectionButton::checkStateSet()
{
}
void SyncDirectionButton::nextCheckState()
{
setCheckState((DirectionState)((checkState()+1)%4));
}
First, the QAbstractButton has 1 "unchecked" state and may have several "checked" states.
This method is called when check state is changed from "unchecked" to "checked". You have to set the inital "checked" state. It should be the first state in your 3 "checked" values,
Also your implementation nextCheckState() should call setChecked(false), when called on 3.rd checked value to return to "unchecked" state.
Betters see the code of QAbstractButton: http://www.koders.com/cpp/fid1779E80AD2DA4C93CA22AB575FAA092A9681AE7B.aspx?s=mdef%3Ainsert
Related
My Xamarin based iOS application should not run in jailbroken devices. This is due to a IS audit. I have implemented the jailbroken detect mechanism. But I cannot find a way to detect whether someone using a jailbroken bypass method like for example A-bypass, shadow tweaks. Anyone with these tweaks can easily by pass the jailbroken detect code.
This is the class I used,
[assembly: Dependency(typeof(CheckHardware))]
namespace CustApp.iOS
{
public class CheckHardware : IHardwareSecurity
{
public CheckHardware()
{
}
public bool IsJailBreaked()
{
if (isPath() || canCreateFile() || isOpenLink())
{
return true;
}
else
{
return false;
}
}
private bool isPath()
{
bool res = false;
List<string> pathList = new List<string>
{
"/Applications/Cydia.app",
"/Applications/Checkra1n.app",
"/Applications/FakeCarrier.app",
"/Applications/Icy.app",
"/Applications/IntelliScreen.app",
"/Applications/MxTube.app",
"/Applications/RockApp.app",
"/Applications/SBSettings.app",
"/Applications/WinterBoard.app",
"/Applications/blackra1n.app",
"/Library/MobileSubstrate/DynamicLibraries/LiveClock.plist",
"/Library/MobileSubstrate/DynamicLibraries/Veency.plist",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/System/Library/LaunchDaemons/com.ikey.bbot.plist",
"/System/Library/LaunchDaemons/com.saurik.Cydia.Startup.plist",
"/etc/apt",
"/private/var/lib/apt",
"/private/var/lib/apt/",
"/private/var/lib/cydia",
"/private/var/mobile/Library/SBSettings/Themes",
"/private/var/stash",
"/private/var/tmp/cydia.log",
"/usr/bin/sshd",
"/var/cache/apt",
"/var/lib/apt",
"/usr/libexec/sftp-server",
"/usr/sbin/sshd",
"/bin/bash",
"/Library/MobileSubstrate/MobileSubstrate.dylib",
"/var/lib/cydia"
};
foreach (var fullPath in pathList)
{
if (File.Exists(fullPath))
{
res = true;
}
}
return res;
}
private bool canCreateFile()
{
try
{
File.WriteAllText("/private/jailbreak.txt", "This is a test.");
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
private bool isOpenLink()
{
if (UIApplication.SharedApplication.CanOpenUrl(NSUrl.FromString("cydia://package/com.example.package")))
{
return true;
}
else
{
return false;
}
}
public bool IsInEmulator()
{
bool isSimulator = Runtime.Arch == Arch.SIMULATOR;
return isSimulator;
}
}
}
In MainActivity.cs I have this method
public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
{
if (keyCode.ToString().Equals("F1"))
{
App.Left = true;
App.Right = false;
}
else if (keyCode.ToString().Equals("F2"))
{
App.Left = false;
App.Right = true;
}
else
{
App.Left = false;
App.Right = false;
}
return base.OnKeyUp(keyCode, e);
}
In my Page class, how can I constantly check whether left or right are true and in case trigger an event?
Create a plugin using Dependency Service and your shared interface should look like this:
public interface IKeyEvent
{
event Action<KeyResult> OnKeyEvent;
}
public enum KeyResult {None, Left, Right};
Implement this for different platforms (Android/iOS/UWP) accordingly and bind it to PCL project. (Check dependency service implementation for help)
Link it to platform-specific KeyUp event.
for android it would be like this:
Droid.KeyEventHandler
[assembly: Dependency(typeof(KeyEventHandler))]
namespace YourNameSpace.Droid
{
public class KeyEventHandler : IKeyEvent
{
public static KeyEventHandler Current;
public KeyEventHandler()
{
Current = this;
}
public event Action<KeyResult> OnKeyResult;
public void RaiseKeyEvent(KeyResult key)
{
OnKeyResult?.Invoke(key);
}
}
}
MainActivity
public override bool OnKeyUp(Keycode keyCode, KeyEvent e)
{
KeyResult keyResult = KeyResult.None; // need to reference YourNameSpace.Shared project to use this enum
if (keyCode == KeyCode.A)
{
keyResult = KeyResult.Left;
}
else if (keyCode == KeyCode.D))
{
keyResult = KeyResult.Right;
}
else
{
keyResult = KeyResult.None;
}
if (KeyEventHandler.Current != null)
{
KeyEventHandler.Current.RaiseKeyEvent(keyResult);
}
return base.OnKeyUp(keyCode, e);
}
NOTE: Left and right keys are mapped to A and D of physical keyboard respectively, for some reason my Macbook's F1/F2 keys were not registering to simulator so I used A/D.
Hope this helps :)
I want to use updateMessage("...") in a Class that is not an Task.
How can I solve this Problem?
Is it possible to give a Task Handle which can perform a updateMessage("...") to a Function?
import javafx.concurrent.Task;
public class TestTask extends Task<Void> {
private final static int COUNT = 1000;
#Override
protected Void call() {
doTaskStuff("Found ");
return null;
}
public void doTaskStuff(String s) {
for (int i = 0; i < COUNT; i++) {
System.out.println(i);
this.updateProgress(i, COUNT);
this.updateMessage(s + (i + 1) + "!");
Dummy.dummy(this, i);
try {
Thread.sleep(1);
} catch (InterruptedException interruptedException) {
}
}
}
}
class Dummy {
public static void dummy(TestTask testTask, int i) {
String s = "";
if (i == 0) {
s = "i == 0";
} else {
s = "i != 0";
}
testTask.updateMessage(s);// does not work
}
}
The issue here is the method being protected. Of course you could override the method and increase the visibility, but this would imho be a bad approach:
#Override
public void updateMessage(String message) {
super.updateMessage(message);
}
Note that the method sets a single message, therefore you could just return the value:
updateMessage(Dummy.dummy(i));
public static String dummy(int i) {
return (i == 0) ? "i == 0" : "i != 0";
}
For multiple updates you could also provide access to this kind of functionality using an interface:
Dummy.dummy(this::updateMessage, i);
public static void dummy(Consumer<String> updater, int i) {
updater.accept((i == 0) ? "i == 0" : "i != 0");
}
I am trying to display the Qt::ForbiddenCursor when I move a mouse over an item in a custom QTreeWidget but only when drop position is QAbstractItemView::OnItem.
Here is the code
void XProjectTreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
QTreeWidgetItem* pItem = itemAt(event->pos());
if (pItem == nullptr)
{
return;
}
XTreeItem* dropItem = dynamic_cast<XTreeItem*>(pItem);
if (dropItem == nullptr)
{
return;
}
XTreeItem::DropPosition drop;
if (!getDropPosition(drop))
{
return;
}
auto items = selectedItems();
if (items.count() == 0)
{
return;
}
auto dragItem = (XTreeItem*)items.first();
if (!dragItem->checkMoveItemPossible(dropItem, drop))
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ForbiddenCursor));
event->setDropAction(Qt::IgnoreAction);
}
else
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
event->setDropAction(Qt::MoveAction);
}
QTreeWidget::dragMoveEvent(event);
}
bool XProjectTreeWidget::getDropPosition(XTreeItem::DropPosition& drop)
{
DropIndicatorPosition dropIndicator = dropIndicatorPosition();
switch (dropIndicator)
{
case QAbstractItemView::AboveItem: drop = XTreeItem::Above; break;
case QAbstractItemView::BelowItem: drop = XTreeItem::Below; break;
case QAbstractItemView::OnItem: drop = XTreeItem::Inside; break;
default: return false;
}
return true;
}
The problem is that the ignore action seems to be applied to all the items with the same type for AboveItem and BelowItem.
dragMoveEvent is called when the DropPosition is OnItem for the items with the same type while hovering the cursor above those items.
How can I show ForbiddenCursor only when mouse is OnItem?
Calling QTreeWidget::dragMoveEvent(event); before changing the cursors fixed the issue. Here is how the fixed code looks now:
void XProjectTreeWidget::dragMoveEvent(QDragMoveEvent * event)
{
// moved this call from the end of the method
QTreeWidget::dragMoveEvent(event);
QTreeWidgetItem* pItem = itemAt(event->pos());
if (pItem == nullptr)
{
return;
}
XTreeItem* dropItem = dynamic_cast<XTreeItem*>(pItem);
if (dropItem == nullptr)
{
return;
}
XTreeItem::DropPosition drop;
if (!getDropPosition(drop))
{
return;
}
auto items = selectedItems();
if (items.count() == 0)
{
return;
}
auto dragItem = (XTreeItem*)items.first();
if (!dragItem->checkMoveItemPossible(dropItem, drop))
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ForbiddenCursor));
event->setDropAction(Qt::IgnoreAction);
}
else
{
QGuiApplication::changeOverrideCursor(QCursor(Qt::ArrowCursor));
event->setDropAction(Qt::MoveAction);
}
}
I am using Caliburn micro(1.3)/MVVM and Silverlight. When I update the itemsource RadGridView, I lose the selected items. I found a blog about implementing a behavior to save the selected items when you are implementing MVVM. I can get the selected items, but I cannot set them back once the itemsource is refreshed. Can someoneshow me how to implement this using caliburn.micro and the RadGridVIew? I think the best way to go is to create a caliburn micro convention, but I can only find a reference for creating a convention for selectedItem, not selectedItems.
Can someone show me how to accomplish this? I tried the following, but it does not work.
private static void SetRadGridSelecteditemsConventions()
{
ConventionManager
.AddElementConvention<DataControl>(DataControl.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
.ApplyBinding = (viewModelType, path, property, element, convention) =>
{
ConventionManager.SetBinding(viewModelType, path, property, element, convention, DataControl.ItemsSourceProperty);
if (ConventionManager.HasBinding(element, DataControl.SelectedItemProperty))
return true;
var index = path.LastIndexOf('.');
index = index == -1 ? 0 : index + 1;
var baseName = path.Substring(index);
foreach (var selectionPath in
from potentialName in ConventionManager.DerivePotentialSelectionNames(baseName)
where viewModelType.GetProperty(potentialName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance) != null
select path.Replace(baseName, potentialName))
{
var binding = new Binding(selectionPath) { Mode = BindingMode.TwoWay };
BindingOperations.SetBinding(element, DataControl.SelectedItemProperty, binding);
}
return true;
};
}
Thanks,
Stephane
You should use a behavior for this since the SelectedItems property is readonly.
Telerik has an example for this, only the example is not specific for caliburn.micro.
If you add the following class to your project:
public class MultiSelectBehavior : Behavior<RadGridView>
{
public INotifyCollectionChanged SelectedItems
{
get { return (INotifyCollectionChanged)GetValue(SelectedItemsProperty); }
set { SetValue(SelectedItemsProperty, value); }
}
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register("SelectedItems", typeof(INotifyCollectionChanged), typeof(MultiSelectBehavior), new PropertyMetadata(OnSelectedItemsPropertyChanged));
private static void OnSelectedItemsPropertyChanged(DependencyObject target, DependencyPropertyChangedEventArgs args)
{
var collection = args.NewValue as INotifyCollectionChanged;
if (collection != null)
{
collection.CollectionChanged += ((MultiSelectBehavior)target).ContextSelectedItems_CollectionChanged;
}
}
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
}
void ContextSelectedItems_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UnsubscribeFromEvents();
Transfer(SelectedItems as IList, AssociatedObject.SelectedItems);
SubscribeToEvents();
}
void GridSelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
UnsubscribeFromEvents();
Transfer(AssociatedObject.SelectedItems, SelectedItems as IList);
SubscribeToEvents();
}
private void SubscribeToEvents()
{
AssociatedObject.SelectedItems.CollectionChanged += GridSelectedItems_CollectionChanged;
if (SelectedItems != null)
{
SelectedItems.CollectionChanged += ContextSelectedItems_CollectionChanged;
}
}
private void UnsubscribeFromEvents()
{
AssociatedObject.SelectedItems.CollectionChanged -= GridSelectedItems_CollectionChanged;
if (SelectedItems != null)
{
SelectedItems.CollectionChanged -= ContextSelectedItems_CollectionChanged;
}
}
public static void Transfer(IList source, IList target)
{
if (source == null || target == null)
return;
target.Clear();
foreach (var o in source)
{
target.Add(o);
}
}
}
This behavior takes care of the synchronization between collection RadGridView.SelectedItems and MultiSelectBehavior.SelectedItems.
Now we need to have an ObservableCollection in the ViewModel
//Collection holding the selected items
private ObservableCollection<object> selectedGridItems;
public ObservableCollection<object> SelectedGridItems
{
get
{
if (selectedGridItems == null)
selectedGridItems = new ObservableCollection<object>();
return selectedGridItems;
}
set
{
if (selectedGridItems == value) return;
selectedGridItems = value;
NotifyOfPropertyChange(() => SelectedGridItems);
}
}
//Deselect all selected items in the gridview
public void ClearSelectedGridItems()
{
SelectedGridItems.Clear();
}
Last thing is bind the behavior in the view
<telerik:RadGridView x:Name="CustomLogs" AutoGenerateColumns="true" SelectionMode="Extended">
<i:Interaction.Behaviors>
<local:MultiSelectBehavior SelectedItems="{Binding SelectedGridItems}"/>
</i:Interaction.Behaviors>
</telerik:RadGridView>
Thats it, hope it helps you!