KeyframeWidget::KeyframeWidget(QWidget* parent) : ConfigWidget(parent) {
  setupUi(this);
  connect(btnSave, SIGNAL(clicked()), this, SLOT(save()));
  connect(btnReload, SIGNAL(clicked()), this, SLOT(reload()));
  connect(btnSaveAs, SIGNAL(clicked()), this, SLOT(saveAs()));
  connect(btnLoad, SIGNAL(clicked()), this, SLOT(load()));
  connect(btnAdd, SIGNAL(clicked()), this, SLOT(addKeyframe()));
  connect(btnDelete, SIGNAL(clicked()), this, SLOT(deleteKeyframe()));
  connect(btnPlay, SIGNAL(clicked()), this, SLOT(play()));
  connect(btnShow, SIGNAL(clicked()), this, SLOT(show()));
  connect(keyframeBox, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(updateItem(QListWidgetItem*)));
  connect(keyframeBox, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(activate(QListWidgetItem*)));
  connect(keyframeBox, SIGNAL(itemSelectionChanged()), this, SLOT(deactivateCurrent()));
  connect(rdoTorso, SIGNAL(toggled(bool)), this, SLOT(supportBaseUpdated(bool)));
  connect(rdoLeftFoot, SIGNAL(toggled(bool)), this, SLOT(supportBaseUpdated(bool)));
  connect(rdoRightFoot, SIGNAL(toggled(bool)), this, SLOT(supportBaseUpdated(bool)));
  connect(rdoSensor, SIGNAL(toggled(bool)), this, SLOT(supportBaseUpdated(bool)));
  keyframeTimer_ = new QTimer(this);
  connect(keyframeTimer_, SIGNAL(timeout()), this, SLOT(playNextFrame()));
  keyframeTimer_->setSingleShot(true);
  activated_ = NULL;
  kfconfig_.sequence_file = UTMainWnd::dataDirectory() + "/kicks/default.yaml";
  
}
bool ofxTLSwitches::mousePressed(ofMouseEventArgs& args, long millis){
    
	if(placingSwitch != NULL){
		if(isActive() && args.button == 0){
			placingSwitch->timeRange.max = millis;
			updateTimeRanges();
		}
		else {
			deleteKeyframe(placingSwitch);
		}
		placingSwitch = NULL;
		return false;
	}
	
	keysAreDraggable = !ofGetModifierSelection();
	
    //check to see if we are close to any edges, if so select them
    bool startSelected = false;
    bool endSelected = false;
    int selectedKeyframeIndex;
    if(isActive() && args.button == 0){
        for(int i = 0; i < keyframes.size(); i++){
            
            ofxTLSwitch* switchKey = (ofxTLSwitch*)keyframes[i];
            //unselect everything else if we just clicked this edge without shift held down
            startSelected = abs(switchKey->display.x - args.x) < 10.0;
            if (startSelected && !switchKey->startSelected && !ofGetModifierSelection()) {
                timeline->unselectAll();
            }
            //Deselect the key if we clicked it already selected with shift held down
            if(ofGetModifierSelection() && ((startSelected && switchKey->startSelected) || isKeyframeSelected(switchKey))){
                switchKey->startSelected = false;    
            }
            else {
                switchKey->startSelected |= startSelected;
            }
            float endEdge = switchKey->display.x+switchKey->display.width;
            endSelected = abs(endEdge - args.x) < 10.0;
            //don't let them both be selected in one click!
            if(!startSelected && endSelected && !switchKey->endSelected && !ofGetModifierSelection()){
                timeline->unselectAll();
            }
            //Deselect the key if we clicked it already selected with shift held down
            if(ofGetModifierSelection() && ((endSelected && switchKey->endSelected) || isKeyframeSelected(switchKey))){
                switchKey->endSelected = false;    
            }
            else{
                switchKey->endSelected |= endSelected && !startSelected;
            }
            
            if(startSelected || endSelected){
				selectedKeyframeIndex = i;
                break;
            }        
        }
    }
    
    //update dragging and snapping if we clicked an edge
    updateEdgeDragOffsets(millis);
    if(endSelected || startSelected){
        ofxTLSwitch* selectedSwitch = (ofxTLSwitch*)keyframes[selectedKeyframeIndex];
        timeline->setDragTimeOffset(selectedSwitch->edgeDragOffset);
    }
	
    if(!endSelected && !startSelected){
    	//normal selection from above

	    ofxTLKeyframes::mousePressed(args, millis);
        if(isActive()){
	        timeline->cancelSnapping(); //don't snap when dragging the whole switch
        }
    }
    
    //move through the keyframes, if both the start and the end have been selected
    //count it as completely selected and let the super class take care of it
    //otherwise if just one of the edges are selected make sure it's unselected
    for(int i = 0; i < keyframes.size(); i++){
        ofxTLSwitch* switchKey = (ofxTLSwitch*)keyframes[i];
        if (switchKey->startSelected && switchKey->endSelected) {
            switchKey->startSelected = switchKey->endSelected = false;
            selectKeyframe(switchKey);
        }
        //make sure that if just one of the edges is clicked that the keyframe is *not* selected
		//also make sure it wasn't *just* selected in the last click by checking that it's not 'the' selected key
        else if( (switchKey->startSelected || switchKey->endSelected) && isKeyframeSelected(switchKey)){
			if(selectedKeyframe == switchKey){
				switchKey->startSelected = switchKey->endSelected = false;
			}
			else{
	            deselectKeyframe(switchKey);
			}
        }
    }
	return false;
}